-->
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.  [ 8 posts ] 
Author Message
 Post subject: Many-To-Many and Association Tables Question
PostPosted: Mon May 10, 2004 10:19 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 9:36 am
Posts: 28
I'm flat out stuck. I've tried to accomplish this several different ways and I've yet to achieve the desired results. I've read through the forums, FAQs, all applicable chapters in the documentation, and googled on it. Alas, I'm just stuck.

I simply want to map a many-to-many relationship that cascades. For example, map the tables users, users_roles, and roles together. From the code I'd like to perform something similar to:

Set roles = new HashSet();
Set users = new HashSet();

User user = new User();
roles.add(new Role(properties..., users));
user.setRoles(roles);

session.save(roles);

or even this would work:

User user = new User();
Role role = new Role();

Set userRoles = new HashSet();
UserRole userRole = new UserRole(user, role);
userRoles.add(userRole);

user.setUserRoles(userRole);
role.setUserRoles(userRole);

session.save(user);
session.save(role)

and have Hibernate writeout the user, role, and the association. I've attempted using a One-To-Many, with cascade="all", inverse="true" set, to user_roles and have gotten Hibernate to save the User and Role, but won't cascade and save the association. I've attempted using a Many-To-Many with cascade="all", inverse="true" set, mapping User to Roles and Roles to Users, but this generated a "SQL insert, update or delete failed (row not found)" exception on the .save() call. Hibernate appears to be trying to update the Role instead of inserting. I've read the FAQ and understand that I should directly call save on the object, but they defeats the purpose of the cascade.

However, in a vain attempt to get this working I did call save on both the User and Role objects and both were inserted into the database. Unfortunatly, the association wasn't. Any help/suggestions/links would be wonderful.

Thank you in advance for your time and help,
Tyler Pitchford


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2004 12:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Check that one http://www.hibernate.org/155.html

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2004 1:14 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 9:36 am
Posts: 28
Thank you, that was the prod in the right direction I needed.

Cheers,
Tyler


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2004 2:56 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
Tyler:

I meet the same problem as yours in many-to-many association mapping and I am stuck. Could you please tell me how you solve your problem? How did you map the relationship, do saving,deleting and updating?

thanx.

Ping


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2004 5:40 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 9:36 am
Posts: 28
Ping,

I had to remove "inverse=true" from one side of the association. I'm writing up a tutorial on it actually. It should be published tomorrow on www.warfrog.com.

Hope that helps,
Tyler Pitchford

pwangb wrote:
Tyler:

I meet the same problem as yours in many-to-many association mapping and I am stuck. Could you please tell me how you solve your problem? How did you map the relationship, do saving,deleting and updating?

thanx.

Ping


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 17, 2004 4:32 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
Tyler:

Thanks for your reply. I can't open the link you provoided though.

I also put inverse=false and it works now. However, it will delete all the records in association table refereced by the deleted records. This is definitely not expected in regular one-to-many relationship. Hence, I think I should set cascade=save_update and inverse=false in one-to-many relationship. Am I right?

I also meet another problem. For example: Student, STUDENT_COURSE and Course. When I try to add a STUDENT_COURSE record say cs, I write: cs.getStudetn().add(cs) and cs.getCourse.add(cs). I expect that the cs record should be added in STUDENT_COURSE table. However, it is not. I have to write: session.save(cs) explicitly. Is it right or I have some problem here?

thanks a lot. Please tell me the link to your tutorial.

Ping


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 17, 2004 8:55 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
Hi Tyler:

I meet another problem.

Let's say in the Student, Student_Course, Course many-to-many relationship. I have DAO for each class.

I have write the following: (pseudocode. cs is a Student_Course object)
student.getStudent_Course().remove(cs);
StudentDAO.update(student);

In StudentDAO update(), I write:
session.update(student);

I found that the reference to cs in student is deleted, however the cs record in table is still there and the Course still has a reference to cs record.

I set up inverse="false" on both Student and Course. I think it is the problem that I don't remove the reference to cs in Course class. However, since I already lose reference to cs before update, how can I solve this problem?

thank you very much.

Ping


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 17, 2004 11:24 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 9:36 am
Posts: 28
Ping,

I'm not totally sure, but i'd suggest setting inverse="true" on one side (either student OR course, but not both). Additionally add 'cascade="all"' to both the course and student associations.

Hope that helps,
Tyler


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