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.  [ 3 posts ] 
Author Message
 Post subject: how to use a bidirectional many-to-many association
PostPosted: Wed Jul 05, 2006 12:54 pm 
Newbie

Joined: Thu Jun 08, 2006 12:10 am
Posts: 3
Location: São Paulo/Brazil
Hello,

I am using hibernate 3.0. I have mapped a bidirectional many to many association beetwen two tables using an association table. My query are working fine, so my mapping seems ok. However, I just don´t know how to create an association by code.
I have classes A and B. A has a property called bSet and b has a property called aSet. If I just use:
A a = session.load(...);
B b = session.load(...);
a.getBSet().add(b); // and/or b.getASet().add(a);
session.save(a);


I get an error saying that there is alredy an object with the same ID. However, I have NO ROWS in my relation table in database.
What am I doing wrong?

_________________
gtalk: mvallebr at gmail.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 4:41 pm 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
You should use save() only for new objects. Since your objects came from persistence (session.load()), you should use session.update(a) instead. Hibernate will insert/delete rows from the link table for you.

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 06, 2006 5:46 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
As hlfsousa said, you should understand that the session is a cache for proxified objects.

So, you could do
Code:
session.beginTransaction();

A a = session.load(...);
B b = session.load(...);
a.getBSet().add(b); // and/or b.getASet().add(a);

session.getTransaction().commit();


Instead of trying to save your object, there's no need to (re)do it.

But, there's no reason why the save raises an exception in your code. When you ask for the save of an object that's already a reference present in the cache, nothing is just done...

So could you be a bit more precise about what you do, your mappings, etc...

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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