-->
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.  [ 3 posts ] 
Author Message
 Post subject: Moving children between parents with all-delete-orphan
PostPosted: Thu Sep 30, 2004 11:01 am 
Newbie

Joined: Wed Sep 29, 2004 7:17 pm
Posts: 2
I have a simple "1:M" mapping between a group and a set of users. I need to move all users from one group to another group. I'm having a difficult time doing this when the cascade for the set of user set to "all-delete-orphan". If I set cascade to "all" this works fine. Is there any way to make this work when cascade is set to "all-delete-orphan"?

BTW: I'm a newbie to Hibernate and am very impressed. Nice job Hibernate team!

Hibernate version: 2.1

Mapping documents:
Code:
<class name="Group" table="my_group">
  <id name="id" type="long" unsaved-value="null" >
    <column name="id" sql-type="long" not-null="true"/>
    <generator class="hilo">
        ... HI LOW PARAMS ...
    </generator>
  </id>

  <!-- PROPERTIES LEFT OUT ... -->

  <set name="users" inverse="true" lazy="true" cascade="all-delete-orphan">
    <key column="group_id"/>
    <one-to-many class="User"/>
  </set>
</class>

<class name="User" table="my_user">
  <id name="id" type="long" unsaved-value="null" >
    <column name="id" sql-type="long" not-null="true"/>
    <generator class="hilo">
       ... HI LOW PARAMS ...
    </generator>
  </id>

  <!-- PROPERTIES LEFT OUT ... -->

  <many-to-one name="group" class="Group" column="group_id" not-null="true"/>
</class>



Code between sessionFactory.openSession() and session.close():
Code:
Group fromGroup = ...load from group ...
Group toGroup = ...load to group ...

transaction = session.newTransaction();
Set users = new HashSet(fromGroup.getUsers());
Iterator it = users.iterator();
while (it.hasNext()) {
    User user = (User)it.next();
    fromGroup.getUsers().remove(user);
    user.setGroup(toGroup);
    toGroup.getUsers().add(user);
}
session.save(fromGroup);
session.save(toGroup);
transaction.commit();



Full stack trace of any exception that occurs:
Code:
CAUSE: deleted object would be re-saved by cascade (remove deleted object from associations): 1002002, of class: User



Name and version of the database you are using: SQL Server

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Code:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 30, 2004 11:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It does not usually make semantic sense to use cascade delete with a many-to-many. Think about the semantics of a many-to-many...


Top
 Profile  
 
 Post subject: Don't think this is many-to-many - is it?
PostPosted: Thu Sep 30, 2004 4:52 pm 
Newbie

Joined: Wed Sep 29, 2004 7:17 pm
Posts: 2
This isn't many-to-many is it? I have a set in Group that's one-to-many and the reverse association that is many-to-one, per the example in your reference document. What am I missing?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.