java.lang.NullPointerException

Hibernate JIRA | Michael Kopp | 8 years ago
tip
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  1. 0

    When saving an entity with a mutable natural id hibernate throws the following NPE. java.lang.NullPointerException at org.hibernate.engine.StatefulPersistenceContext.getNaturalIdSnapshot(StatefulPersistenceContext.java:267) at org.hibernate.event.def.DefaultFlushEntityEventListener.checkNaturalId(DefaultFlushEntityEventListener.java:78) at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:162) at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113) at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) I checked the code and found the reason: - getNaturalIdSnapshot calls getDatabaseSnapshot and checks the result for NO_ROW but not for null - getDatabaseSnapshot will never return NO_ROW but null if nothing was found hence getNaturalIdSnapshot will produce a NPE if the entity is not in the database already. the fix is simple: change: Object[] entitySnapshot = getDatabaseSnapshot( id, persister ); if ( entitySnapshot == NO_ROW ) { return null; } to Object[] entitySnapshot = getDatabaseSnapshot( id, persister ); if ( entitySnapshot == null ) { return null; } This is still there in trunk as well!

    Hibernate JIRA | 8 years ago | Michael Kopp
    java.lang.NullPointerException
  2. 0

    When saving an entity with a mutable natural id hibernate throws the following NPE. java.lang.NullPointerException at org.hibernate.engine.StatefulPersistenceContext.getNaturalIdSnapshot(StatefulPersistenceContext.java:267) at org.hibernate.event.def.DefaultFlushEntityEventListener.checkNaturalId(DefaultFlushEntityEventListener.java:78) at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:162) at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113) at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) I checked the code and found the reason: - getNaturalIdSnapshot calls getDatabaseSnapshot and checks the result for NO_ROW but not for null - getDatabaseSnapshot will never return NO_ROW but null if nothing was found hence getNaturalIdSnapshot will produce a NPE if the entity is not in the database already. the fix is simple: change: Object[] entitySnapshot = getDatabaseSnapshot( id, persister ); if ( entitySnapshot == NO_ROW ) { return null; } to Object[] entitySnapshot = getDatabaseSnapshot( id, persister ); if ( entitySnapshot == null ) { return null; } This is still there in trunk as well!

    Hibernate JIRA | 8 years ago | Michael Kopp
    java.lang.NullPointerException

    Root Cause Analysis

    1. java.lang.NullPointerException

      No message provided

      at org.hibernate.engine.StatefulPersistenceContext.getNaturalIdSnapshot()
    2. Hibernate
      StatefulPersistenceContext.getNaturalIdSnapshot
      1. org.hibernate.engine.StatefulPersistenceContext.getNaturalIdSnapshot(StatefulPersistenceContext.java:267)
      1 frame
    3. Hibernate
      SessionImpl.flush
      1. org.hibernate.event.def.DefaultFlushEntityEventListener.checkNaturalId(DefaultFlushEntityEventListener.java:78)
      2. org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:162)
      3. org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
      4. org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
      5. org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
      6. org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
      7. org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
      7 frames