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>Preferences>Java>Code Generation>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>Preferences>Java>Code Generation>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