Hallo,
ich versuche mir gerade ein Hibernate setup zu basteln. Aber unweigerlich bleibt mein EntityManger null.
Vielleicht seht ihr den Fehler?
Stacktrace:
main:
DAO:
Person:
persistence:
Hoffentlich wisst ihr etwas...
ich versuche mir gerade ein Hibernate setup zu basteln. Aber unweigerlich bleibt mein EntityManger null.
Vielleicht seht ihr den Fehler?
Stacktrace:
Java:
Exception in thread "main" java.lang.NullPointerException
at model.PersonDAO.create(PersonDAO.java:22)
at model.main.main(main.java:10)
main:
Java:
package model;
public class main {
public static void main(String[] args){
Person person = new Person();
person.setName("Hibernate JPA Person");
System.out.println("Person id before creation: " + person.getId());
PersonDAO personDAO = new PersonDAO();
Person persistedCustomer = personDAO.create(person);
System.out.println("Person id after creation: " + persistedCustomer.getId());
}
}
DAO:
Java:
package model;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ejb.Stateless;
@Stateless
public class PersonDAO {
@PersistenceContext
private EntityManager em;
public Person create(Person newPerson) {
em.getTransaction().begin(); //<-- exception
em.persist(newPerson);
em.getTransaction().commit();
return newPerson;
}
}
Person:
Java:
package model;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
import org.hibernate.annotations.Entity;
@Entity
public class Person implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name = "";
public int getId() {
return id;
}
public void setId(short id) {
this.id = id;
}
public Person(String name) {
this.name = name;
}
public Person() {
}
public String toString() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
persistence:
Java:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="HolidayCalculatorDB" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.connection.username" value="" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:MyDB" />
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop"/> <!-- update -->
</properties>
</persistence-unit>
</persistence>
Hoffentlich wisst ihr etwas...