org.hibernate.id.IdentifierGenerationException: null id generated for:class com.vladmihalcea.hibernate.masterclass.laboratory.cascade.CascadeLockTest$PostDetails

Hibernate JIRA | Vlad Mihalcea | 2 years ago
  1. 0

    I have the following mapping: # A Parent {{Post}} entity {code:java} @Entity(name = "Post") public class Post { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; @OneToOne(cascade = CascadeType.ALL, mappedBy = "post", optional = false, fetch = FetchType.LAZY) private PostDetails details; public String getName() { return name; } public void setName(String name) { this.name = name; } public PostDetails getDetails() { return details; } public void addDetails(PostDetails details) { this.details = details; details.setPost(this); } } {code} # A Child {{PostDetails}} entity: {code:java} @Entity(name = "PostDetails") public class PostDetails { @Id private Long id; private Date createdOn; public PostDetails() { createdOn = new Date(); } @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "id") @MapsId private Post post; public Long getId() { return id; } public void setPost(Post post) { this.post = post; } } {code} When I try to cascade the persist operation from Post to PostDetails: {code:java} Post post = new Post(); post.setName("Hibernate Master Class"); post.addDetails(new PostDetails()); session.persist(post); {code} I get the following error: {noformat} org.hibernate.id.IdentifierGenerationException: null id generated for:class com.vladmihalcea.hibernate.masterclass.laboratory.cascade.CascadeLockTest$PostDetails at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:119) at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:801) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:794) at org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:314) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118) at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:432) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) {noformat} If I make the association as optional: {code:java} @OneToOne(cascade = CascadeType.ALL, mappedBy = "post", optional = true, fetch = FetchType.LAZY) {code} The persist works properly.

    Hibernate JIRA | 2 years ago | Vlad Mihalcea
    org.hibernate.id.IdentifierGenerationException: null id generated for:class com.vladmihalcea.hibernate.masterclass.laboratory.cascade.CascadeLockTest$PostDetails
  2. 0

    I have the following mapping: # A Parent {{Post}} entity {code:java} @Entity(name = "Post") public class Post { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; @OneToOne(cascade = CascadeType.ALL, mappedBy = "post", optional = false, fetch = FetchType.LAZY) private PostDetails details; public String getName() { return name; } public void setName(String name) { this.name = name; } public PostDetails getDetails() { return details; } public void addDetails(PostDetails details) { this.details = details; details.setPost(this); } } {code} # A Child {{PostDetails}} entity: {code:java} @Entity(name = "PostDetails") public class PostDetails { @Id private Long id; private Date createdOn; public PostDetails() { createdOn = new Date(); } @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "id") @MapsId private Post post; public Long getId() { return id; } public void setPost(Post post) { this.post = post; } } {code} When I try to cascade the persist operation from Post to PostDetails: {code:java} Post post = new Post(); post.setName("Hibernate Master Class"); post.addDetails(new PostDetails()); session.persist(post); {code} I get the following error: {noformat} org.hibernate.id.IdentifierGenerationException: null id generated for:class com.vladmihalcea.hibernate.masterclass.laboratory.cascade.CascadeLockTest$PostDetails at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:119) at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:801) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:794) at org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:314) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118) at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:432) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) {noformat} If I make the association as optional: {code:java} @OneToOne(cascade = CascadeType.ALL, mappedBy = "post", optional = true, fetch = FetchType.LAZY) {code} The persist works properly.

    Hibernate JIRA | 2 years ago | Vlad Mihalcea
    org.hibernate.id.IdentifierGenerationException: null id generated for:class com.vladmihalcea.hibernate.masterclass.laboratory.cascade.CascadeLockTest$PostDetails
  3. 0

    null id generated for:class com.jpa.project.model.domain.Item

    Stack Overflow | 1 month ago | user122090
    javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.jpa.project.model.domain.Item
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Exception in thread "main" org.hibernate.id.IdentifierGenerationException: null id generated for:class iland.hbm.BillProductDetails

    Stack Overflow | 3 years ago | xrcwrn
    org.hibernate.id.IdentifierGenerationException: null id generated for:class iland.hbm.BillProductDetails
  6. 0

    How do I properly cascade save a one-to-one, bidirectional relationship on primary key in Hibernate 3.6

    Stack Overflow | 6 years ago | Mike Lively
    javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.sellingsource.bizdev.entities.LeadAffiliate

    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.id.IdentifierGenerationException

      null id generated for:class com.vladmihalcea.hibernate.masterclass.laboratory.cascade.CascadeLockTest$PostDetails

      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId()
    2. Hibernate
      SessionImpl.persist
      1. org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:119)
      2. org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
      3. org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
      4. org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:801)
      5. org.hibernate.internal.SessionImpl.persist(SessionImpl.java:794)
      6. org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:314)
      7. org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
      8. org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
      9. org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
      10. org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
      11. org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:432)
      12. org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
      13. org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
      14. org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
      15. org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
      16. org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
      17. org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
      18. org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
      19. org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
      20. org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
      20 frames