持久化实体时出现NullPointerException

时间:2013-04-17 11:16:37

标签: java spring hibernate

我能够使用NamedQuery成功检索对象列表。如果我不对对象进行任何修改然后尝试合并它(使用我的实体管理器),则不会抛出任何异常。但是,如果我更新该实体中的任何字段并尝试合并它,则会抛出以下异常:

SEVERE: Servlet.service() for servlet [spring-mvc] in context with path [/EuropCar]   threw exception [Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction] with root cause
java.lang.NullPointerException
    at org.hibernate.ejb.event.EJB3PostUpdateEventListener.handlePostUpdate(EJB3PostUpdateEventListener.java:71)
    at org.hibernate.ejb.event.EJB3PostUpdateEventListener.onPostUpdate(EJB3PostUpdateEventListener.java:67)
    at org.hibernate.action.internal.EntityUpdateAction.postUpdate(EntityUpdateAction.java:248)
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:205)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:276)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:328)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1233)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)

我检查了以下内容:

     
  • 我坚持的实体不是空的。  
  • entityManager不为null。
非常感谢任何帮助。


// Code to update the 'Rate' entity
Rate r = rateDao.getRate();
r.setCountry("NA");
rateDao.update(r);

// RateDaoImpl code (DAO IMPLEMENTATION CLASS)
@Override
public Rate getRate() {
// named query is : "SELECT rp FROM RatePeriod rp WHERE rp.id = 458"
return em.createNamedQuery("Rate.qry", Rate.class).getResultList().get(0);
}

@Transactional(readOnly=false)
@Override
public void update(Rate rate) {
em.merge(rate);
}

2 个答案:

答案 0 :(得分:17)

我在同一行中有同样的例外。那是因为我使用了不兼容的版本: 休眠核心:4.2.0.Final 冬眠-的EntityManager:4.1.9.Final

我已将hibernate-core降级为4.1.9.Final并且问题消失了。

答案 1 :(得分:2)

这是EJB3PostUpdateEventListener的代码:

70  private void handlePostUpdate(Object entity, EventSource source) {
71      EntityEntry entry = (EntityEntry) source.getPersistenceContext().getEntry( entity );
72      // mimic the preUpdate filter
73      if ( Status.DELETED != entry.getStatus()) {
74          callbackHandler.postUpdate(entity);
75      }
76  }

在此方法中设置断点,查看null第71行的内容。也许getPersistenceContext()会返回null