-->
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: Persisting many-to-many associations
PostPosted: Mon Dec 13, 2004 11:27 pm 
Newbie

Joined: Mon Dec 13, 2004 10:36 pm
Posts: 1
I've gone through the FAQ's, read the first 3 chapters of King/Bauer's book and browsed Wrox's Hibernate book but I'm still a little unsure about many-to-many relationships. According to King and Bauer, its better to steer clear of these if possible and instead "use two one-to-many associations for either side....[and use] an intermediate association class."
(page 225). In that spirit, Ive proceeded. To take a simple example from my app, I've got a many to many relationship between users and permissions (a user can have many permissions and a permission can be assigned to many users). Following King/Bauer I built an intermediate class called PermissionUser and can persist permissions, users, and the relationship between these objects. However, to assign a permission to a user and persist the user so that its permissions are also saved, I'm finding that I cant just add the permission to the user and save it. Instead I also have to:

1)instantiate an intermediate class (permissionuser)
2)assign the intermediate class to user *and* permission
3)save the the user *and* the permission class

I'm enclosing the relevant code below:

1)PermissionUser permissionuser = new PermissionUser();
2)permissionuser.setPermission(permission);
3)permissionuser.setUser(user);
4)user.addPermissionuser(permissionuser);
5)permission.addPermissionuser(permissionuser);
6)aSession.saveOrUpdate(user);
7)aSession.saveOrUpdate(permission);
8)aSession.flush();


Is there an easier way? This approach seems to have a bit too much coding overhead....ideally I'd like to just be able to assign the permissionuser object to the user object and save the user object. I was hoping ORM would spare me the trouble of having to perform the same operations on the permission object (e.g. steps 5 and 7).

(Actually in an ideal world I'd like to just assign the permission object to the the user object and get rid of the intermediate "association class"-- but that doesn't seem practical in light of King/Bauer's recommends on page 225).

Cheers,

Luke Fernandez
Weber State University



Hibernate version:2.1.4

Mapping documents:


<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="gen.Permission"
table="PERMISSION"
>

<!-- Common id property. -->
<id name="id"
type="long"
column="PERMISSION_ID"
unsaved-value="null">
<generator class="native"/>
</id>

<property name="name" />


<set
name="permissionusers"
inverse="true"
cascade="all-delete-orphan">
<key column="PERMISSION_ID"/>
<one-to-many class="gen.PermissionUser"/>
</set>

</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="gen.PermissionUser"
table="PERMISSION_USER"
>

<!-- Common id property. -->
<id name="id"
type="long"
column="PERMISSIONUSER_ID"
unsaved-value="null">
<generator class="native"/>
</id>

<many-to-one
name="permission"
class="gen.Permission"
column="PERMISSION_ID"
update="false"
not-null="true"
outer-join="false"
foreign-key="FK1_PERMISSION_ID"/>


<many-to-one
name="user"
class="gen.User"
column="USER_ID"
update="false"
not-null="true"
outer-join="false"
foreign-key="FK1_USER_ID"/>




</class>

</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="gen.User"
table="USER"
>

<!-- Common id property. -->
<id name="id"
type="long"
column="USER_ID"
unsaved-value="null">
<generator class="native"/>
</id>

<property name="firstname" />
<property name="lastname" />
<property name="email" />
<property name="login" />
<property name="password" />

<set
name="teamusers"
inverse="true"
cascade="all-delete-orphan">
<key column="USER_ID"/>
<one-to-many class="gen.TeamUser"/>
</set>

<set
name="permissionusers"
inverse="true"
cascade="all-delete-orphan">
<key column="USER_ID"/>
<one-to-many class="gen.PermissionUser"/>
</set>

</class>
</hibernate-mapping>





Name and version of the database you are using: MySQL


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.