-->
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: update many-to-many relationship manually
PostPosted: Fri Dec 22, 2006 2:49 pm 
Newbie

Joined: Tue Oct 25, 2005 6:11 am
Posts: 5
Location: Hamburg, Germany
Hello,

I would like to update a n:m relationship only when I say so.

I have 3 tables user, userrole, role so every user can own several roles.
The problem is, that every time I update the user all connections of the user within userrole will be deleted and than reinserted.

Is there a way with inverse="false" and cascade="none" to call a method that saves the relationship only when it was changed.

This way it is working, but creates deletes and inserts on every login, because I update the loginLast Field:
Code:
<class name="RbacUser" table="rbac_user" dynamic-update="true" lazy="false">
   <id name="id" column="id_user" type="long">
      <generator class="native"/>
   </id>
   <version name="version"/>
   <property name="name" column="name" type="string" not-null="true"/>
   <property name="loginLast" column="loginlast" type="java.util.Date" not-null="false"/>
   <bag name="roles" cascade="all-delete-orphan" lazy="false" table="rbac_userrole">
      <key>
         <column name="id_user" length="20" not-null="true"/>
      </key>
      <many-to-many class="RbacRole">
         <column name="id_role" length="20" not-null="true"/>
      </many-to-many>
   </bag>
</class>


The role does not reference the user, because there are too many users:
Code:
<class name="RbacRole" table="rbac_role" dynamic-update="true" lazy="true">
   <id name="id" column="id_role" type="long">
      <generator class="native"/>
   </id>
   <property name="name" column="name" type="string" not-null="true"/>
</class>


This assigns the roles and uses a simple update of the user entity:
Code:
public void assignRoles(RbacUser user, Long[] roleids) throws RbacException
{
    List<RbacRole> roles = new ArrayList<RbacRole>(getRbacRoleDao().findByIds(roleids));
    user.setRoles(roles);
    getSession().saveOrUpdate(user);
}


I tried this:
Code:
<class name="RbacUser" table="rbac_user" dynamic-update="true" lazy="false">
...
   <bag name="roles" cascade="none" inverse="true" lazy="false" table="rbac_userrole">
...
   </bag>
</class>

public void assignRoles(RbacUser user, Long[] roleids) throws RbacException
{
    List<RbacRole> roles = new ArrayList<RbacRole>(getRbacRoleDao().findByIds(roleids));
    user.setRoles(roles);
    getSession().saveOrUpdate(user.getRoles());
}

But this threw an exception.

Does anyone know if a direct call to update a many-to-many relationship is possible at all.

Thanful for any help.

Torsten


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.