持久化bean

时间:2015-04-22 17:35:57

标签: java-ee jpa wildfly-8

我使用wildfly 8.2,javaee 7和H2数据库。当我试图坚持或合并一个实体时,我得到了NPE。以下是我的课程。你知道为什么吗?

@Entity
public class Patient implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String firstName;
    private String lastName;
    private Date DOB;

//Getters/Setters here
}

我的EJB服务。 findAll()工作正常。

@Stateless
public class PatientService {

    @PersistenceContext(unitName="patient-pu")
    private EntityManager em;

    public List<Patient> findAll() {
        CriteriaQuery<Patient> cq = em.getCriteriaBuilder().createQuery(Patient.class);
        cq.select(cq.from(Patient.class));
        return em.createQuery(cq).getResultList();
    }

    public void saveOrPersist(Patient entity) {
        if (entity.getId() > 0) {
            em.merge(entity);
        } else {
            em.persist(entity);
        }
    }

}

的persistence.xml

<?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="patient-pu" transaction-type="JTA">
    <jta-data-source>java:jboss/h2</jta-data-source>
    <class>com.example.backend.Patient</class>
  </persistence-unit>
</persistence>

我的用户界面。我使用Vaadin框架

@Override
    protected void init(VaadinRequest vaadinRequest) {

        //Create new record
        Patient patient = new Patient();
        patient.setFirstName("John");
        patient.setLastName("Doe");
        patient.setDOB(new Date());
        patientService.saveOrPersist(patient); 

    } 

这是我遇到的错误

10:02:05,549 ERROR [org.jboss.as.ejb3.invocation] (default task-6) JBAS014134: EJB Invocation failed on component PatientService for method public void com.example.backend.PatientService.saveOrPersist(com.example.backend.Patient): javax.ejb.EJBException: java.lang.NullPointerException
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:190) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:340) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
    at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
    at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:95) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
    at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:3

0 个答案:

没有答案