-->
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.  [ 1 post ] 
Author Message
 Post subject: ManyToOne Relationship Causing A New Object To Be Created
PostPosted: Sun Apr 04, 2010 10:21 am 
Newbie

Joined: Thu Apr 16, 2009 7:09 am
Posts: 13
Hi,

I have a strange situation, not sure if anyone has seen this before, but I have a transient object, A, and want to set the relationships on this object, so I set the ManyToOne relationship by using the set method for B, and then I ensure that B has an instance of A in its collection of A, but when I do this it creates a new instance of A, so then it tries to insert 2 instances of A into the database, which obviously throws a Unique constraint error. I put a breakpoint on the constructor of A, and it is called when I call getB().getSetOfA().add(A), Any ideas as to why it tries to create a new instance of A?

I am very puzzled by this, so is this usual? Or has someone seen anything like this before?

I can't post any code for this at the moment, as I don't have it available to me, but I can post some more details tomorrow if it helps.

Thanks in advance,
Osian

p.s. Another thing I would like to know, is why, when using a manytomany relationship, do you have to use merge and not persist when you have pre-existing objects for the manytomany in the system? Is there an annotation that I can use to avoid having to use merge? as I know that the other side of the manytomany will always be pre-existing data in the database.


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

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.