我有一个Java hibernate错误

时间:2016-11-18 11:59:35

标签: java hibernate jpa intellij-idea

我尝试使用hibernate和JPA连接 h2 数据库和网络应用,但我收到此error 我在名为Students的h2数据库中创建名为Person的表。 我使用了IntelliJ IDEA,将JPA,Hibernate和H2集成到新的WebProject中。

这是我的 persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="StudentPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/Students"/>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.connection.username" value="doncho"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>
            <property name="hibernate.temp.use_jdbc_metadata_defaults"
                      value="false" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
        </properties>
    </persistence-unit>
</persistence>

我的豆子:

package com.beans;
import javax.persistence.*;    
@javax.ejb.Stateless(name = "StudentBeanEJB")
@Entity
@Table(name = "Person")
public class StudentBean { 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column
    private String userName;
    @Column
    private String passWord;
    @Column
    private String email;

    public StudentBean() {
    } 
    public String getUserName() {
        return userName;
    }   
    public void setUserName(String userName) {
        this.userName = userName;
    }   
    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

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

我尝试使用AddPerson类添加新学生:

public class AddPerson {

    public static void main(String [ ] args){

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("StudentPU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();


        StudentBean addStudent = new StudentBean();
        addStudent.setId(0);
        addStudent.setUserName("username");
        addStudent.setPassWord("pass");
        addStudent.setEmail("d1521@abv.com");

        em.persist(addStudent);
        em.getTransaction().commit();

        em.close();
        emf.close();

    }

}

我第一次尝试用hibernate“持久化”而且我很困惑如何正确配置persistence.xml文件。 我很高兴有任何想法来解决这个错误。

致以最诚挚的问候,

d。 Balamjiev

0 个答案:

没有答案