实体管理器JPA persist确实在创建的表中插入数据

时间:2016-12-08 11:43:53

标签: hibernate jpa entity entitymanager persist

java通过wildfly正确连接到数据库并在数据库中创建表。使用ManagerCount,我想将测试数据插入数据库。  “     包hm.edu.chatServer.service;

import java.io.Serializable;
import javax.persistence.Column;
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;

    @Column(name="Benutzername")
    private String userName;


    @Column(name="numberOfReceivedConfirms")
    private int numberOfReceivedConfirms;

    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>
      <properties>
           <!-- Properties for Hibernate -->
         <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>
        }
    }
    '

这是我的ManagerCount.java:     “     包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.persist(CountProcess);

        return CountProcess;
    }
}
'

当我想将测试数据插入到创建的表中时,我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这样:

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

您可以通过创建 CountProcess 类型的对象在您的 servlet 中进行测试,并使用您的测试数据填充它,然后从的实例调用您的create方法ManagerCount