-->
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: How to automatically remove links from a Collection?
PostPosted: Wed Jun 09, 2010 6:22 am 
Newbie

Joined: Wed Jun 09, 2010 5:54 am
Posts: 5
I have a class 'User' and a class 'Group'. Each user has many groups and group has many users. What is the best way to set this relationship up such that the Group.users collection is automatically updated when a User is deleted?

For example, assume the following group is setup.
Code:
// Add users to a group
User tom = getUser("tom");
User joe = getUser("joe");
Group test = getGroup("test");
test.getUsers().add(tom);
test.getUsers().add(joe);
session.save(test);

Later in time, remove a user. What I would like is to automatically update the test.users collection such that tom is removed. Is this possible? Or am I supposed to manage the test.users collection and explicitly remove tom from the collection before trying to delete that User?
Code:
User tom = getUser("tom");
session.delete(tom);

At the moment using the collection mapping given below, when I try to delete this object a ConstraintViolationException is generated as the foreign key is violated
Code:
Integrity constraint violation FK25560AA4876FD071 table: GROUPUSERS_USER_SET in statement [/* delete org.pcj.model.User */ delete from Users where ID=? and Version=?]

The mapping is given below:
Code:
<class name='Group' table='Groups' dynamic-insert='true' dynamic-update='true' optimistic-lock='version'>
  <id name='Id' column='ID'>
   <generator class='native'/>
  </id>
  <natural-id mutable='false'>
   <property name='Name'/>
  </natural-id>
  <version name='Version'/>
  <property name='Description'/>
  <set name='Users' table='GroupUsers_User_set' cascade='all'>
   <key column='GroupUsers_ID'/>
   <many-to-many class='User' column='User_ID'/>
  </set>
</class>


Top
 Profile  
 
 Post subject: Re: How to automatically remove links from a Collection?
PostPosted: Wed Jun 09, 2010 10:08 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
There is a constraint violation. Look into the log files of your database and find out what this constraint violation constrains. Give the mapping of your User class.

regards garz


Top
 Profile  
 
 Post subject: Re: How to automatically remove links from a Collection?
PostPosted: Wed Jun 09, 2010 12:51 pm 
Newbie

Joined: Wed Jun 09, 2010 5:54 am
Posts: 5
Thanks for your reply. The foreign key constraint exception is triggered because the collection group.users holds a reference to the primary key for User. Attempt to remove the User fails because that would leave a dangling reference in the collection to a user that no longer exists. The User mapping is shown below.

Code:
<?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 package='org.pcj.model'>
<class name='User' table='Users' dynamic-insert='true' dynamic-update='true' optimistic-lock='version'>
  <id name='Id' column='ID'>
   <generator class='native'/>
  </id>
  <natural-id mutable='false'>
   <property name='Pager'/>
  </natural-id>
  <version name='Version'/>
  <property name='Firstname'/>
  <property name='Lastname'/>
  <property name='Email'/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: How to automatically remove links from a Collection?
PostPosted: Thu Jun 10, 2010 5:25 am 
Beginner
Beginner

Joined: Wed Jul 01, 2009 8:11 am
Posts: 34
i think what you are trying to achieve is not possible, but i'm not too experienced with Hibernate which means i could be wrong.

as far as i understood this you have to remove the user from the collection and also the user itself. though you can use cascade style "all-delete-orphan" to let Hibernate delete the user when it's removed from the collection. the cascade style "all" that you are using now only says that the user is deleted when the group is deleted.

Hibernate tries to imitate Java behaviour. if you set a variable that had a reference to a user to null, thus deleting it, the collection that contained the user would still hold a reference to it. therefor you also had to remove the user from the collection.

anybody correct me if i'm wrong. :)


Top
 Profile  
 
 Post subject: Re: How to automatically remove links from a Collection?
PostPosted: Fri Jun 11, 2010 3:25 pm 
Newbie

Joined: Wed Jun 09, 2010 5:54 am
Posts: 5
Thank you for your reply. Unless I hear otherwise, I will design it so that I explicity remove it from the collection. If anyone knows of a good way to manage these back-references within an application in an automatic fashion. let me know.

Paul


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.