• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

JPA - Getting error while saving Parent Entity and Child Entity need to be saved separately.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am in a very typical business usecase scenario w.r.t Hibernate Entities

I have a parent entityA that has a list with EntityB

Parent EntityA
no <---- generated by sequence in DB
name
List<EntityB>



EntityB
Embeddable Entity
name
address



EmbeddedEntityC
no  <----------------- this is EntityA.no
XXX
YYY




EntityA.no will only be generated adter record is persisted in EntityA.

If I use, cascade = CascadeType.ALL -  while saving EntityA, since my dependent Entities are not ready (EntityA.no is not available) I can't save them
If I don't use cascade = CascadeType.ALL - when I am trying to save child Entity, I am getting "unsaved transient instance" error

I am looking for a way to get this design to work using Hibernate
 
Saloon Keeper
Posts: 24325
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To ensure proper operation, you must cross-link the parent and all children, both in annotation and in actual value assignment. You must also persist both parent and children within the scope of the same transaction.

And, if you later modify the collection of children, you must do it all again.
 
Pratibha Dev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for responding. I have cross link between Parent and Child. My issue however is
EntityA encloses EntityB, so, EntityA record can’t be saved until EntityB is ready to be persisted.
On other hard, EntityB will not be ready until  EntityA.seqId is generated by Database Sequence Generator.
So,this leads to something similar to Deadlock.

I have spend lot of time today to explore various ways to get around this. Nothing from “Detaching Entity”, @PrePersist/@PostPersist seems to be helpful.
 
Saloon Keeper
Posts: 13281
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cross-posted at https://stackoverflow.com/questions/61219018/hibernate-save-parent-entity-first-and-child-entity-later-but-child-entity-depe

Pratibha, please notify all sites where you've posed your question of all other sites where you've posed your question. See BeForthrightWhenCrossPostingToOtherSites.
 
Tim Holloway
Saloon Keeper
Posts: 24325
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pratibha Dev wrote:Thanks for responding. I have cross link between Parent and Child. My issue however is
EntityA encloses EntityB, so, EntityA record can’t be saved until EntityB is ready to be persisted.
On other hard, EntityB will not be ready until  EntityA.seqId is generated by Database Sequence Generator.
So,this leads to something similar to Deadlock.

I have spend lot of time today to explore various ways to get around this. Nothing from “Detaching Entity”, @PrePersist/@PostPersist seems to be helpful.



I've dealt with that problem myself and the easiest way to handle it is not to use the internal sequence generator.
 
Pratibha Dev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for answering, I am fairly new to JPA. I have code snippet to help me understand.  
The Hierarchy is Test.CustomField.CustomFieldId

This is the piece where I save my data









 
Get me the mayor's office! I need to tell her about this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic