org.hibernate.PropertyValueException: not-null property references a null or transient value: MyEntity.firstPersistedOn

Hibernate JIRA | Johan Steiner | 1 decade ago
  1. 0

    [EJB-46] PrePersist callback method not called if entity's primary key is null - Hibernate JIRA

    atlassian.net | 1 year ago
    org.hibernate.PropertyValueException: not-null property references a null or transient value: MyEntity.firstPersistedOn
  2. 0

    Hi, the description is from http://forum.hibernate.org/viewtopic.php?t=944964 but I'm experiencing the exact same issue. ********************* Hibernate does property validation such as not null checking before it does the EJB3 callback to prepersist/preupdate. I'm not sure if there's a good reason for this, but I think it would be particularly convenient if this behavior was reversed. IMHO it seems to better fit the semantics of the PRE callbacks, and it would allow callbacks to make modifications to the objects before they are persisted or updated -- modifications that might in turn effect the property validation Hibernate is doing. The "audit" example in the entity manager documentation does make changes to the object. What if these changes had effected the property validation done before the callback occurred? What if the object was in an invalid state before the callback, but a valid state after the callback? The latter case is what I think would be conveniently handled if hibernate did its property validation after prepersist/preupdate. Just two cents worth, obviously there are workarounds. This EJB3 stuff is looking great. Ryan P.S. This might also allow those of us who assign our own IDs to objects to do so automatically within a callback. ********************* In my case I'm working with an entity like: public class MyEntity { @Basic(temporalType = TemporalType.TIMESTAMP) @Column(name = "$createdOn", insertable = true, updatable = false, nullable = false) private Date firstPersistedOn = null; @Basic(temporalType = TemporalType.TIMESTAMP) @Column(name = "$modifiedOn", insertable = true, updatable = false, nullable = true) private Date lastPersistedOn = null; @PrePersist public void onPrePersist() { firstPersistedOn = new Date(); } @PreUpdate public void onPreUpdate() { lastPersistedOn = new Date(); } } Hibernate throws: org.hibernate.PropertyValueException: not-null property references a null or transient value: MyEntity.firstPersistedOn at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:262) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:164) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114) at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:167) at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:113) at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:60) at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:540) at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:139) Regards, Johan

    Hibernate JIRA | 1 decade ago | Johan Steiner
    org.hibernate.PropertyValueException: not-null property references a null or transient value: MyEntity.firstPersistedOn
  3. 0

    Hi, the description is from http://forum.hibernate.org/viewtopic.php?t=944964 but I'm experiencing the exact same issue. ********************* Hibernate does property validation such as not null checking before it does the EJB3 callback to prepersist/preupdate. I'm not sure if there's a good reason for this, but I think it would be particularly convenient if this behavior was reversed. IMHO it seems to better fit the semantics of the PRE callbacks, and it would allow callbacks to make modifications to the objects before they are persisted or updated -- modifications that might in turn effect the property validation Hibernate is doing. The "audit" example in the entity manager documentation does make changes to the object. What if these changes had effected the property validation done before the callback occurred? What if the object was in an invalid state before the callback, but a valid state after the callback? The latter case is what I think would be conveniently handled if hibernate did its property validation after prepersist/preupdate. Just two cents worth, obviously there are workarounds. This EJB3 stuff is looking great. Ryan P.S. This might also allow those of us who assign our own IDs to objects to do so automatically within a callback. ********************* In my case I'm working with an entity like: public class MyEntity { @Basic(temporalType = TemporalType.TIMESTAMP) @Column(name = "$createdOn", insertable = true, updatable = false, nullable = false) private Date firstPersistedOn = null; @Basic(temporalType = TemporalType.TIMESTAMP) @Column(name = "$modifiedOn", insertable = true, updatable = false, nullable = true) private Date lastPersistedOn = null; @PrePersist public void onPrePersist() { firstPersistedOn = new Date(); } @PreUpdate public void onPreUpdate() { lastPersistedOn = new Date(); } } Hibernate throws: org.hibernate.PropertyValueException: not-null property references a null or transient value: MyEntity.firstPersistedOn at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:262) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:164) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114) at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:167) at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:113) at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:60) at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:540) at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:139) Regards, Johan

    Hibernate JIRA | 1 decade ago | Johan Steiner
    org.hibernate.PropertyValueException: not-null property references a null or transient value: MyEntity.firstPersistedOn
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Hibernate Community • View topic - not-null property references a null or transient value error

    hibernate.org | 4 months ago
    org.hibernate.PropertyValueException: not-null property references a null or transient value: participate.model.questionnaire.Page._pagesBackref
  6. 0

    Hibernate Community • View topic - not-null property references a null or transient value error

    hibernate.org | 6 months ago
    org.hibernate.PropertyValueException: not-null property references a null or transient value: participate.model.questionnaire.Page._pagesBackref

    1 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. org.hibernate.PropertyValueException

      not-null property references a null or transient value: MyEntity.firstPersistedOn

      at org.hibernate.engine.Nullability.checkNullability()
    2. Hibernate
      Nullability.checkNullability
      1. org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
      1 frame
    3. Hibernate
      SessionImpl.merge
      1. org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:262)
      2. org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:164)
      3. org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
      4. org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:167)
      5. org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:113)
      6. org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:60)
      7. org.hibernate.impl.SessionImpl.merge(SessionImpl.java:540)
      7 frames
    4. Hibernate EJB
      AbstractEntityManagerImpl.merge
      1. org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:139)
      1 frame