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