I ve been reading and reconfiguring for the last 2 days about parent children relationships. The problem is the following:
I have a user (parent) whish has children roles as set and one profile. Please see the maping files below.
I have the scenario where I need to update roles:
oldRoles = user.getUserRoles();
it = newRoles.iterator();
obj=null;
while(it.hasNext()){
obj = it.next();
if(!oldRoles.contains(obj)){
oldRoles.add(obj);
System.out.println(((UserRole)obj).getComp_id().getRolename()+" --------");
}
else{
;//do nothing
}
}
user.setUserRoles(oldRoles);
session.update(user);
session.flush();
The above peace is for testing purpose. Anyway, the system.out is executed meaning that I am adding new roles. But roles are never updated in the dataase, even with hibernate, I cann't see any update statement. Before the code above I have another peace where I update the profile and it goes through successfully. Any ideas.....Thanks in advance
-------------------------------------------------------------
mapping files
-------------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="UserRole"
table="USER_ROLES"
>
<composite-id name="comp_id" class="UserRolePK">
<key-property
name="username"
column="USERNAME"
type="java.lang.String"
length="20"
/>
<key-property
name="rolename"
column="ROLENAME"
type="java.lang.String"
length="20"
/>
</composite-id>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- bi-directional many-to-one association to Role -->
<many-to-one
name="role"
class="Role"
update="false"
insert="false"
>
<column name="ROLENAME" />
</many-to-one>
<!-- bi-directional many-to-one association to User -->
<many-to-one
name="user"
class="User"
update="false"
insert="false"
>
<column name="USERNAME" />
</many-to-one>
<!-- end of derived association(s) -->
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="User"
table="USERS"
schema="WQBEMP"
>
<meta attribute="implement-equals" inherit="false">true</meta>
<id
name="username"
type="java.lang.String"
column="USERNAME"
>
<generator class="assigned" />
</id>
<property
name="password"
type="java.lang.String"
column="PASSWORD"
length="20"
/>
<!-- Associations -->
<one-to-one name="userProfile"
class="UserProfile"
cascade="save-update"
/>
<!-- bi-directional one-to-many association to UserProfile -->
<!-- bi-directional one-to-many association to UserRole -->
<set
name="userRoles"
lazy="false"
cascade="all"
inverse="true"
>
<key>
<column name="USERNAME" />
</key>
<one-to-many
class="UserRole"/>
</set>
</class>
</hibernate-mapping>