-->
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 updating 2nd table removes reference in 1st
PostPosted: Mon Mar 28, 2005 4:03 pm 
Newbie

Joined: Fri Mar 11, 2005 7:22 pm
Posts: 10
Example
Table User (fields: name, desc)
Table Items (fields: itemname, desc)
Table User_Item_Crosswalk = Crosswalk between A and B.

In User, I assign instances of Items through the crosswalk table
Everything works fine in hibernate. so lets say
User1 has Items A, B, and C

However, if I change any property of an item (the name or description, it deletes the reference that User1 has of that item. (ie it deletes the record from the crosswalk table)

Any idea why this would occur?
The only property I have on the .hbm.xml on the set attribute is to lazy="true"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 4:34 pm 
Regular
Regular

Joined: Thu Apr 15, 2004 1:12 pm
Posts: 55
Can you post the relevant Hibernate mappings?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 7:12 pm 
Newbie

Joined: Fri Mar 11, 2005 7:22 pm
Posts: 10
well my example was a dumb'down version of what I was coding. Here is the maping file for the user

<?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>
<class
name="SecurityUserDO"
table="security_user">

<id
name="id"
column="user_id"
type="java.lang.Long">
<generator class="increment"/>
</id>

<property name="userName" column="user_name" type="string" not-null="true" />
<property name="password" column="user_password" type="string" not-null="true" />

<property name="firstName" column="first_name" type="string" not-null="true" />
<property name="lastName" column="last_name" type="string" not-null="true" />
<property name="email" column="email" type="string" not-null="true" />

<property name="inactive" column="inactive" type="boolean" not-null="true" />
<property name="system" column="system" type="boolean" not-null="true" />

<property name="simpleSearchString" column="simple_search" type="string" not-null="true" />

<property name="createdBy" column="created_by" type="string" not-null="true" />
<property name="createdStamp" column="created_stamp" type="timestamp" not-null="true" />
<property name="updatedBy" column="updated_by" type="string" not-null="true" />
<property name="updatedStamp" column="updated_stamp" type="timestamp" not-null="true" />

<set name="securityPermissions" table="security_user_permission_crswlk" lazy="true">
<key column="user_id" />
<many-to-many column="permission_id" class="SecurityPermissionDO" />
</set>

<set name="securityRoles" table="security_user_role_crswlk" lazy="true">
<key column="user_id" />
<many-to-many column="role_id" class="SecurityRoleDO" />
</set>
</class>
</hibernate-mapping>


And then for the role (which when edited, is being removed from the user)

<?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>
<class
name="SecurityRoleDO"
table="security_role">

<id
name="id"
column="role_id"
type="java.lang.Long">
<generator class="increment"/>
</id>

<property name="roleName" column="role_name" type="string" not-null="true"/>
<property name="roleDesc" column="role_desc" type="string" not-null="true"/>

<property name="inactive" column="inactive" type="boolean" not-null="true" />
<property name="system" column="system" type="boolean" not-null="true" />

<property name="simpleSearchString" column="simple_search" type="string" not-null="true" />

<property name="createdBy" column="created_by" type="string" not-null="true" />
<property name="createdStamp" column="created_stamp" type="timestamp" not-null="true" />
<property name="updatedBy" column="updated_by" type="string" not-null="true" />
<property name="updatedStamp" column="updated_stamp" type="timestamp" not-null="true" />


<set name="securityUsers" table="security_user_role_crswlk" lazy="true" >
<key column="role_id" />
<many-to-many column="user_id" class="SecurityUserDO" />
</set>

<set name="securityPermissions" table="security_role_permission_crswlk" >
<key column="role_id" />
<many-to-many column="permission_id" class="SecurityPermissionDO" />
</set>
</class>
</hibernate-mapping>

On doing an update to Role, the following stastements are debug 'd

15:10:48,464 DEBUG BasicCollectionPersister:479 - Deleting collection: [SecurityRoleDO.securityUsers#8]
15:10:48,464 DEBUG BatcherImpl:204 - about to open: 0 open PreparedStatements, 0 open ResultSets
15:10:48,464 DEBUG SQL:230 - delete from security_user_role_crswlk where role_id=?
Hibernate: delete from security_user_role_crswlk where role_id=?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 7:22 pm 
Regular
Regular

Joined: Thu Apr 15, 2004 1:12 pm
Posts: 55
The Javadoc for the Set API has this to say:

Quote:
Note: Great care must be exercised if mutable objects are to be used as set elements. The behavior of a Set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element of the set.


Could you be running afoul of this stipulation of the Set contract?

—ml—


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 8:01 pm 
Newbie

Joined: Fri Mar 11, 2005 7:22 pm
Posts: 10
Ahhh i figured it out.
When I was editing the role, the list of users was set to null, thus it removed all the references to user.


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.