Uploaded image for project: 'Hibernate ORM'
  1. HHH-10727

org.hibernate.PropertyValueException: not-null property exception when trying to persist an entity object with null value for each of the property marked with nullable = true

    Details

    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true

      Description

      @Entity
      // Other annotations
      public class RelatedEntities
      {
      	// ... other mappings
      	
      	@ManyToOne( fetch = FetchType.LAZY )
          @JoinColumn( name = "entity1__id", updatable = true, columnDefinition = "int(10)" , nullable = false )
          private Entity1 entity1;
          
          @ManyToOne( fetch = FetchType.LAZY )
          @JoinColumn( name = "entity2__id", updatable = true, columnDefinition = "int(10)" , nullable = true )
          private Entity2 entity2;
          
          @ManyToOne( fetch = FetchType.LAZY )
          @JoinColumn( name = "entity3__id", updatable = true, columnDefinition = "int(10)" , nullable = true )
          private Entity3 entity3;
          
      
          
      }
      
      @Entity
      // Other annotations
      public class Entity1
      {
      	// ... other mappings
      	
      	@OneToMany( mappedBy = "entity1", fetch = FetchType.LAZY )
          @BatchSize( size = 1024 )
          private Set<RelatedEntities> relatedEntities = new HashSet<>();
      
      }
      
      @Entity
      // Other annotations
      public class Entity2
      {
      	// ... other mappings
      	
      	@OneToMany( mappedBy = "entity2", fetch = FetchType.LAZY )
          @BatchSize( size = 1024 )
          private Set<RelatedEntities> relatedEntities = new HashSet<>();
          
        
      }
      
      @Entity
      // Other annotations
      public class Entity3
      {
      	// ... other mappings
      	
      	@OneToMany( mappedBy = "entity3", fetch = FetchType.LAZY )
          @BatchSize( size = 1024 )
          private Set<RelatedEntities> relatedEntities = new HashSet<>();
      }
      

      When trying to persist an object of RelatedEntities with null value for entity2, hibernate throws following exception:

      	org.hibernate.PropertyValueException: not-null property references a null or transient value :  example.RelatedEntities.entity2
      	at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:92)
      	at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:115)
      	at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:69)
      	at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:597)
      	at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:232)
      	at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:213)
      	at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:256)
      	at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318)
      	at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275)
      	at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
      	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
      	at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
      	at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
      	at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
      	at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778)
      	at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751)
      	at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:756)	
      

      Similarly when trying to persist an object of RelatedEntities with null value for entity3, hibernate throws same exception for example.RelatedEntities.entity3:

      	org.hibernate.PropertyValueException: not-null property references a null or transient value :  example.RelatedEntities.entity3
      

      Above piece of code works perfectly with hibernate version 4.3.8.Final

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: