-->
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.  [ 4 posts ] 
Author Message
 Post subject: many-to-many foreign key error
PostPosted: Tue Dec 01, 2009 4:53 am 
Newbie

Joined: Tue Dec 01, 2009 4:43 am
Posts: 3
Dear all,

I have a many-to-many relationship between two object: A and B, using an association table AB. I can properly load objects and navigate the relationships. I have set cascade="all" in the many-to-many relationship.

I'm creating a new element in table A, adding to the many-to-many association a B object as shown in the pseudo-code below:

A a = new A();
a.setName ("test");

B b = session.load(B.class, id);

a.getBs().add(b);

session.save(a);
session.commit();[/i]

The issue is that hibernate store in the AB table a new row but it uses "0" as foreign key to the new A object. It seems that hibernate doesn't know the A's id (that is saving) and so it put 0 generating a foreign key violation.

How can I avoid this?

Thanks in advance,

Rosario Alfano


Top
 Profile  
 
 Post subject: Re: many-to-many foreign key error
PostPosted: Tue Dec 01, 2009 5:43 am 
Newbie

Joined: Sat Sep 26, 2009 5:18 am
Posts: 3
hi
yes ... maybe ...
try to new method in A
Code:
public addB(B b) {
   b.setA(this);
   myBCollection.add(b);
}


Top
 Profile  
 
 Post subject: Re: many-to-many foreign key error
PostPosted: Tue Dec 01, 2009 5:35 pm 
Newbie

Joined: Tue Dec 01, 2009 4:43 am
Posts: 3
nothing to do. It continues to put 0 as foreign key to the added object.


Top
 Profile  
 
 Post subject: Re: many-to-many foreign key error
PostPosted: Wed Dec 02, 2009 5:19 am 
Newbie

Joined: Tue Dec 01, 2009 4:43 am
Posts: 3
Dear all,

I solved the issue. It was due to the generator class="assigned" I had. I changed it into generator class="native" and it worked properly.

Regards,

ros


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.