0

i use wildfly 8.2, javaee 7 and H2 database. When I try to persist or merge an entity, I got NPE. Below are my classes. Do you know why?

@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
}

My EJB service. The findAll() works fine.

@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>

My UI. I use Vaadin framework

@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); 

    } 

here's the errors I've got

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
Andy
  • 77
  • 1
  • 12
  • just a blind shot: `em` was not injected so it's null while persisting – wypieprz Apr 22 '15 at 20:28
  • how can I inject `em` please? – Andy Apr 22 '15 at 21:33
  • Wildfly should inject (initialize) `em` automatically. If indeed `em == null` there may be a configuration problem. – wypieprz Apr 22 '15 at 21:40
  • Rookie mistake :) It's because my Id is a Long so now I changed it to != null then it's gotten past this error. Now I got another error `Sequence "HIBERNATE_SEQUENCE" not found; SQL statement`. I want value for Id to be incremented automatically as `strategy = GenerationType.AUTO`. Is that correct? – Andy Apr 22 '15 at 22:40
  • Yes, it should be enough for automatic id generation. Anyway please take a look at this topic: [How to choose the id generation strategy when using hibernate](http://stackoverflow.com/questions/10041938/how-to-choose-the-id-generation-strategy-when-using-hibernate) – wypieprz Apr 22 '15 at 22:47

0 Answers0