-->
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.  [ 5 posts ] 
Author Message
 Post subject: many-to-many problem
PostPosted: Tue May 04, 2004 3:33 pm 
Newbie

Joined: Sat May 01, 2004 2:48 pm
Posts: 13
Location: Sweden
Hi!

I am having a problem with a many-to-many relationship.

First, the...
..(involved) DB-TABLES:

OFSUser -- (m:n) -- Role
An additional table, UserRole, is used to map the m:n relationship.


MAPPING FILE:
<class name="OFSUser" table="ofsuser">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<property name="email" type="string">
<meta attribute="finder-method">findByMail</meta>
</property>
<property name="password" type="string"/>

<set name="roles" table="userrole" cascade="all">
<key column="userid"/>
<many-to-many column="roleid" class="edu.ofs.database.dto.Role"/>
</set>
</class>

<class name="UserRole" table="userrole">
<id column="userid" name="userid" type="long">
<generator class="native"/>
</id>
<property name="roleid" type="long"/>
</class>


<class name="Role" table="role">
<id column="id" name="id" type="long">
<generator class="native"/>
</id>
<property name="description" type="string">
<meta attribute="finder-method">findByDescription</meta>
</property>
</class>


RELEVANT JAVA SOURCE CODE:

OFSUser newuser = new OFSUser();
newuser.setEmail(mail);
newuser.setPassword(pw);

Set newroles = new HashSet();
// [...] add some new roles to the set

newuser.setRoles(newroles);
session.save(newuser);


THE PROBLEM:
The object OFSUser is stored correctly in the database (MySQL), but shouldn't a new entry to table UserRole be added automatically too, based on the many-to-many declaration? Maybe my mapping file is flawed or do I overestimate Hibernate's capabilities. Thus, it would be necessary to add a new entry to UserRole manually.

Thanks in advance + Regards,
- Markus


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 3:52 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
// [...] add some new roles to the set


do you save those roles?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 3:59 pm 
Newbie

Joined: Sat May 01, 2004 2:48 pm
Posts: 13
Location: Sweden
Hi!

Thanks for the immediate reply. That's terrific in this forum.

No, I don't save these roles explicitely. And I also don't want to save them. I just want to have these entries in UserRole.

The function this problems belongs to is "add a new user". so i only want to assign user to his roles, not add new ones in the DB.

Example:
new user(john@somedomain.com, mypassword) + selected role: CEO

Then I want hibernate to add an entry in UserRole, so
userid: 1 (for john) + role id 1 (for CEO)

Regards,
- Markus


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 4:05 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
but what are these roles? are you :
- instanciating new one
- retrieving the role and then adding the roles into the collection?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 04, 2004 4:09 pm 
Newbie

Joined: Sat May 01, 2004 2:48 pm
Posts: 13
Location: Sweden
the second option:
retrieving the role and then adding the roles into the collection?


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