-->
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.  [ 2 posts ] 
Author Message
 Post subject: object added to more than one one-to-many assoc.
PostPosted: Wed Feb 11, 2004 12:45 pm 
Newbie

Joined: Fri Jan 09, 2004 10:24 am
Posts: 7
Can I expect hibernate to detect if I add the same object to more than one one-to-many association ?

Suppose I have a one-to-many association 'owns' between class Person + Car
a Person 'owns' many Cars, a Car is owned by one Person.

Person.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="test">
   <class name="Person" table="Person">      
      <id name="id" type="integer" unsaved-value="null">
         <column name="id" sql-type="numeric(10,0)"/>
         <generator class="native"/>
      </id>
      <property name="name" type="string"/>
      
      <set name="owns" lazy="true">
         <key>
            <column name="person_owns" sql-type="numeric(10,0)"/>
         </key>
         <one-to-many class="Car"/>
      </set>
      
   </class>
</hibernate-mapping>


Car.hbm.xml
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="test">
   <class name="Car" table="Car">      
      <id name="id" type="integer" unsaved-value="null">
         <column name="id" sql-type="numeric(10,0)"/>
         <generator class="native"/>
      </id>
      <property name="model" type="string"/>
   </class>
</hibernate-mapping>


Person.java
Code:
package test;

/**
* @author gcoghlan
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class Person
{
   private Integer id;
   private java.util.Set owns;
   private String name;
   
   /**
    * @return
    */
   public Integer getId()
   {
      return id;
   }

   /**
    * @param integer
    */
   public void setId(Integer integer)
   {
      id = integer;
   }

   /**
    * @return
    */
   public java.util.Set getOwns()
   {
      return owns;
   }

   /**
    * @param set
    */
   public void setOwns(java.util.Set set)
   {
      owns = set;
   }

   /**
    * @return
    */
   public String getName()
   {
      return name;
   }

   /**
    * @param string
    */
   public void setName(String string)
   {
      name = string;
   }

}


Car.java
Code:
package test;

/**
* @author gcoghlan
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class Car
{
   private Integer id;
   private String model;
   
   /**
    * @return
    */
   public Integer getId()
   {
      return id;
   }

   /**
    * @param integer
    */
   public void setId(Integer integer)
   {
      id = integer;
   }

   /**
    * @return
    */
   public String getModel()
   {
      return model;
   }

   /**
    * @param string
    */
   public void setModel(String string)
   {
      model = string;
   }

}


I had the hope that if I add the same car to more than one 'owns' set, then the second one should fail/cause an exception.
sample code:
Code:
      Person person1 = new Person();
      person1.setName("person1");
      Person person2 = new Person();
      person2.setName("person2");
      Car car1 = new Car();
      car1.setModel("model1");
      Car car2 = new Car();
      car2.setModel("model2");
      Session session = openSession();
      try
      {
         session.save(person1);
         session.save(person2);
         session.save(car1);
         session.save(car2);
      
         person1.setOwns(new java.util.HashSet());
         person2.setOwns(new java.util.HashSet());
         person1.getOwns().add(car1);
         person1.getOwns().add(car2);
         session.save(person1);
      
         person2.getOwns().add(car1);
         /* person1 still contains car1, so shouldn't this cause a problem ?*/
         session.save(person2);
         
         session.flush();
         session.connection().commit();
      }
      finally
      {
         session.close();
      }
   


is this possible ?
maybe I need to make the association bi-directional ?

thanks,
Grainne


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Basically, this can't be done because you may use Hibernate in a cluster.
Adding the bidir and using inverse="true" prevent this kind of issue by design (see the reference guide and the wiki area on inverse=true) but don't raise exception.

_________________
Emmanuel


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