Jakarta ee 持久化bean时的nullpointerexception
我使用wildfly 8.2、JavaEE7和H2数据库。当我试图持久化或合并一个实体时,我得到了NPE。下面是我的课程。你知道为什么吗Jakarta ee 持久化bean时的nullpointerexception,jakarta-ee,jpa,wildfly-8,Jakarta Ee,Jpa,Wildfly 8,我使用wildfly 8.2、JavaEE7和H2数据库。当我试图持久化或合并一个实体时,我得到了NPE。下面是我的课程。你知道为什么吗 @Entity public class Patient implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String firstName; private S
@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);
}
}
}
这是我的错误
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
只是一个盲点:
em
没有被注入,所以在持久化时它是空的。我如何才能注入em
请?Wildfly应该自动注入(初始化)em
。如果确实em==null
可能存在配置问题。新手错误:)因为我的Id很长,所以现在我将其更改为!=空,那么它就通过了这个错误。现在我得到另一个错误序列“HIBERNATE\u Sequence”没有找到;SQL语句
。我希望Id的值自动递增为strategy=GenerationType.AUTO
。是吗?是的,它应该足以自动生成id。无论如何,请看一下这个主题:
@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