timsa7
timsa7

Reputation: 133

Persisting two entities that refer to one

enter image description here

I have three entities as shown in the image.

I am trying to persist the objects as shown in this pseudo code

EA = new EA
EB = new EB
EC = new EC
EB.link(EA)
EC.link(EA)
persist(EB)
persist(EC)

When persisting Entity B, Entity A get persisted but then Entity C fails to persist with the following error:

java.lang.IllegalStateException: org.hibernate.TransientObjectException: object is an unsaved transient instance - save the transient instance before merging: 

I control entity C. I would not like to change anything in Entities A and B. How can I get all these entities to be persisted?

Upvotes: 0

Views: 73

Answers (2)

timsa7
timsa7

Reputation: 133

enter image description here

I had to change the model for it to work nicely.

the pseudo-code is

EA=new EA
EB=new EB
EC=new EC
EB.link(EA)
EB.link(EC)
persist(EB)

Upvotes: 0

zbrunson
zbrunson

Reputation: 1757

I think this will work:

EA = new EA
EB = new EB
EC = new EC
EB.link(EA)
persist(EB)
EC.link(EB.getEA())
persist(EC)

Upvotes: 1

Related Questions