I tried to connect h2 database and web app using hibernate and JPA, but I receive this error I create table with name Person in h2 database with name Students. I was used IntelliJ IDEA with JPA, Hibernate and H2 integration for the new WebProject.
This is my persistence.xml file:
<?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>
My Bean:
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;
}
}
And I tried to add new student with class 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();
}
}
I tried to "persist" with hibernate for first time and I'm little confused how to configure persistence.xml file correctly. I would be grateful for any ideas to resolve this error.
Best regards,
D. Balamjiev