Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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: 21964
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11882
253
  • Mark post as helpful
  • send pies
  • 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: 21964
149
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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









 
We noticed he had no friends. So we gave him this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic