Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when persisting entity

I am successfully able to retrieve a list of objects using a NamedQuery. If I do not make any modifications to an object and then attempt to merge it (using my entity manager), no exception will be thrown. However, if I update any field within that entity and attempt to merge it, the following exception is thrown:

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)

I have checked the following:

  • that the entity I'm persisting is not null.
  • that the entityManager is not null.
Any assistance would be greatly appreciated.
// 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);
}
like image 330
Stephen Paul Avatar asked Apr 17 '13 11:04

Stephen Paul


2 Answers

I had the same exception in the same line. That was because I have used incompatible versions: hibernate-core:4.2.0.Final hibernate-entitymanager:4.1.9.Final

I have downgraded hibernate-core to 4.1.9.Final and the problem disappeared.

like image 51
Raigedas Avatar answered Sep 27 '22 21:09

Raigedas


This is the code of 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  }

Set a breakpoint in this method and see what is null line 71. Maybe getPersistenceContext() returns null.

like image 40
Kai Avatar answered Sep 27 '22 19:09

Kai