-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Saving Entity with foreign key to existing record.
PostPosted: Mon Dec 18, 2017 9:28 am 
Newbie

Joined: Mon Dec 18, 2017 9:18 am
Posts: 1
I am new to hibernate. I am using version 4.3.8.
An easy question for a seasoned user.

I have record A with two foreign keys to record B.primaryKey and Record C.primaryKey.
The layout of record A looks like this.
A.id
A.foreignKey to B.primaryKey
A.foreignKey to C.primaryKey
A.fieldX


Record B currently exists in the system.

Record C is a new record I want to save when I save record A.

What does the entity class for A look like and how do I get A to save without trying to cascade a save for B?

Thanks for your help.


Top
 Profile  
 
 Post subject: Re: Saving Entity with foreign key to existing record.
PostPosted: Wed Dec 20, 2017 5:42 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
A is the child entity. Both B and C are its parents.

So, you should never cascade an entity state transaction from a child to a parent because the parent should exist prior to creating the child.

For more details, check out this article.

To solve your issue, either you cascade the persist from B to A if you map the relationship on B as well.

Or just issue two persist operations, one for B and one for A.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.