-->
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 Object from One Collection to Another
PostPosted: Mon Feb 26, 2007 12:50 pm 
Newbie

Joined: Fri Sep 29, 2006 12:42 pm
Posts: 4
Moving an object from one container to another is a very common maneuver (think cut-and-paste). I have a situation where i would like to use 'cascade="all-delete-orphan"'. This works just fine until i try to move an object from one collection to another, at which time i receive an exception stating "deleted object would be re-saved by cascade". The object in question is not orphaned -- it's in the another collection. If i switch to 'cascade="all"', i create true orphans that remain in the DB anytime i remove an object from a collection for good.

Should this situation be reported as a bug? The "would be re-saved" phrase is ironic, as it is exactly the behavior that i need, since the moved object is not really an orphan.

If this is something around which we must work, has the community declared any best-practices? (Removing the object from collection-1 and adding a clone of it to collection-2 is not an option here; i must deal with the same reference. Also, the DAO layer will continue operate only on the holder of the collections, not on any of the objects lower in the containment hierarchy.)

A very simplified (and not entirely analogous) example follows.


Code:
/** An object that has two Hands. */
public class Person
{
  private Long id;
 
  private String name;
  public  String getName()             { return name; }
  public  void   setName(String name)  { this.name = name; }

  private Hand leftHand;
  public  Hand getLeftHand()  { return leftHand; }

  private Hand rightHand;
  public  Hand getRightHand()  { return rightHand; }
 
  public Person()  { this("Jane Doe"); }

  public Person(String name)
  {
    this.name = name;
    leftHand  = new Hand("Left Hand");
    rightHand = new Hand("Right Hand");
  }
}


Code:
/** A container of marbles. */
public class Hand
{
  private Long id;
 
  private String name;
  public  String getName()             { return name; }
  public  void   setName(String name)  { this.name = name; }
 
  private List<Marble> marbles;
  public  List<Marble> getMarbles()  { return marbles; }
 
  public Hand()  { this ("unnamed"); }
 
  public Hand(String name)
  {
    this.name = name;
    marbles = new ArrayList<Marble>();
  }
}


Code:
/** A simple object that can be held in a Hand. */
public class Marble
{
  private Long id;
 
  private String name;
  public  String getName()             { return name; }
  public  void   setName(String name)  { this.name = name; }
 
  public Marble()  { this("unnamed"); }
 
  public Marble(String name)  { this.name = name;}
}


Code:
/** Program to move marbles from one hand to another. */
public class Main
{
  public static void main(String[] args)
  {
    //Create the objects and save to DB
    Person person = new Person();
    Hand   leftHand  = person.getLeftHand();
    Hand   rightHand = person.getRightHand();
   
    List<Marble> marbles = leftHand.getMarbles();
    for (int m=0; m < 10; m+=2)
      marbles.add(new Marble("m"+m));
   
    marbles = rightHand.getMarbles();
    for (int m=1; m < 10; m+=2)
      marbles.add(new Marble("m"+m));
   
    PersonDao dao = new PersonDao();
    dao.save(person);

    //Remove some marbles from leftHand
    leftHand.getMarbles().remove(0);
    leftHand.getMarbles().remove(0);
    dao.save(person);

    //Move a marble from rightHand to leftHand
    //THIS IS THE SECTION THAT FAILS IF cascade="all-delete-orphan"
    leftHand.getMarbles().add(rightHand.getMarbles().remove(0));
    dao.save(person);
  }
}


Code:
//DAO layer not shown here


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="marbles">
   
   <class name="Person">
      <id name="id" type="long" access="field">
         <generator class="native"/>
      </id>
            
      <property name="Name"/>
      
      <many-to-one name="leftHand"  class="Hand" column="Left_Hand_ID"  cascade="all" access="field"/>
      <many-to-one name="rightHand" class="Hand" column="Right_Hand_ID" cascade="all" access="field"/>
   </class>
   
   <class name="Hand">
      <id name="id" type="long" access="field">
         <generator class="native"/>
      </id>
      
      <property name="Name"/>

      <list name="marbles" cascade="all-delete-orphan" access="field">
         <key column="Hand_ID"/>
         <list-index column="List_Position"/>
         <one-to-many class="Marble"/>
      </list>
   </class>
   
   <class name="Marble">
      <id name="id" type="long" access="field">
         <generator class="native"/>
      </id>

      <property name="Name"/>
   </class>
   
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 2:58 pm 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
Interesting, just today I've come across the same problem.
So are there any solutions or workarounds available?

Thanks,
Holger


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 4:16 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Search JIRA, has been reported before. Not easy to change, provide a patch if you can.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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.