EntityManager Hibernate JPA将数据插入表中

时间:2016-12-07 22:26:33

标签: hibernate jpa java-ee-6 wildfly-8

我已经设法通过Wildfly连接到两个MySQL数据库。并通过JPA自动为每个数据库创建一个包含其列的表。现在我的问题是我无法自动将数据插入数据库。

这是我的ManagerTrace.java类:

package hm.edu.chatServer.service;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class ManagerTrace {

    @PersistenceContext(unitName="tracetabelle")
    private EntityManager emt;

    public TraceProcess save(TraceProcess TraceProcess){
        emt.getTransaction().begin();
        emt.persist(TraceProcess);
        emt.getTransaction().commit();
        emt.close();

        return TraceProcess;
    }
}

`

这是我的TraceProcess.java类:

package hm.edu.chatServer.service;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity // markiert Klasse als Entität
public class TraceProcess {

    @Id // Primary Key Attribut
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

      // Name des Client-Threads, der den Request absendet
      private String clientThreadName="test";

      // Name des Threads, der den Request im Server
      private String serverThreadName="test";

      // Nutzdaten (eigentliche Chat-Nachricht in Textform)
      private String message="test";

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getClientThreadName() {
        return clientThreadName;
    }

    public void setClientThreadName(String clientThreadName) {
        this.clientThreadName = clientThreadName;
    }

    public String getServerThreadName() {
        return serverThreadName;
    }

    public void setServerThreadName(String serverThreadName) {
        this.serverThreadName = serverThreadName;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
`

这是我的ManagerCount.java类:

package hm.edu.chatServer.service;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless  
public class ManagerCount {

    @PersistenceContext(unitName="counttabelle")
    private EntityManager emc;

    public CountProcess create(CountProcess CountProcess){ 
        emc.getTransaction().begin();
        emc.persist(CountProcess);
        emc.getTransaction().commit();
        emc.close();

        return CountProcess;
    }
}
`

这是我的CountProcess.java类:

package hm.edu.chatServer.service;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@SuppressWarnings("serial")
@Entity // markiert Klasse als Entität
public class CountProcess implements Serializable { 

    @Id //  Primary Key Attribut
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int id;

    // Login-Name des Clients  
    private String userName ="testuser";

    // Anzahl an empfangenen Bestaetigungen der anderen Clients
    private int numberOfReceivedConfirms=2;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getNumberOfReceivedConfirms() {
        return numberOfReceivedConfirms;
    }

    public void setNumberOfReceivedConfirms(int numberOfReceivedConfirms) {
        this.numberOfReceivedConfirms = numberOfReceivedConfirms;
    }
}

最后我的persistence.xml:

<?xml version="1.0" encoding="utf-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="counttabelle" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/countDS</jta-data-source>
    <class>hm.edu.chatServer.service.CountProcess</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="url" value="jdbc:mysql://localhost/count?createDatabaseIfNotExist=true"></property>
      <property name="hibernate.archive.autodetection" value="ManagerCount" />
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    </properties>
  </persistence-unit>
  <persistence-unit name="tracetabelle" transaction-type="JTA">
    <jta-data-source>java:jboss/datasources/traceDS</jta-data-source>
    <class>hm.edu.chatServer.service.TraceProcess</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="url" value="jdbc:mysql://localhost/trace?createDatabaseIfNotExist=true"></property>
      <property name="hibernate.archive.autodetection" value="ManagerTrace" />
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    </properties>
  </persistence-unit>
</persistence>

0 个答案:

没有答案