我编写了两个持久化类:OneToOneLeft和OneToOneRight
他们之间遵循one to one 映射
我做了如下操作:
Session session = config.buildSessionFactory().openSession();
Transaction tran = session.beginTransaction();
OneToOneLeft left1 = new OneToOneLeft();
OneToOneLeft left2 = new OneToOneLeft();
OneToOneRight right1 = new OneToOneRight();
left1.setOneToOneRight(right1);
right1.setOneToOneLeft(left1);
left2.setOneToOneRight(right1);
right1.setOneToOneLeft(left2);
session.save(left1);
session.save(left2);
session.save(right1);
tran.commit();
////////////////////////////////////////
Transaction tran2 = session.beginTransaction();
/** 得到left1对象 */
OneToOneLeft left_1 = (OneToOneLeft) session.load(OneToOneLeft.class, new Long(1));
tran2.commit();
但是经过测试发现left_1里面的oneToOneRight属性仍然指向right1,这样正常么?
(我认为由于操作:
left1.setOneToOneRight(right1);
right1.setOneToOneLeft(left1);
left2.setOneToOneRight(right1);
right1.setOneToOneLeft(left2);
应该已经使left1的oneToOneRight对象为空了)
请前辈们指点一下,谢谢。
他们之间遵循one to one 映射
我做了如下操作:
Session session = config.buildSessionFactory().openSession();
Transaction tran = session.beginTransaction();
OneToOneLeft left1 = new OneToOneLeft();
OneToOneLeft left2 = new OneToOneLeft();
OneToOneRight right1 = new OneToOneRight();
left1.setOneToOneRight(right1);
right1.setOneToOneLeft(left1);
left2.setOneToOneRight(right1);
right1.setOneToOneLeft(left2);
session.save(left1);
session.save(left2);
session.save(right1);
tran.commit();
////////////////////////////////////////
Transaction tran2 = session.beginTransaction();
/** 得到left1对象 */
OneToOneLeft left_1 = (OneToOneLeft) session.load(OneToOneLeft.class, new Long(1));
tran2.commit();
但是经过测试发现left_1里面的oneToOneRight属性仍然指向right1,这样正常么?
(我认为由于操作:
left1.setOneToOneRight(right1);
right1.setOneToOneLeft(left1);
left2.setOneToOneRight(right1);
right1.setOneToOneLeft(left2);
应该已经使left1的oneToOneRight对象为空了)
请前辈们指点一下,谢谢。
第 6 章 对象/关系数据库映射基础(Basic O/R Mapping)
中的
6.1.11. 一对一
章节.
[OneToOneLeft.hbm.xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "./hbm/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.qqlove.hibernate.pojo.OneToOneLeft" table="OneToOneLeft">
<id name="id" column="id" type="long">
<generator class="increment"></generator>
</id>
<property name="leftProperty1" column="leftProperty1" type="string" />
<property name="leftProperty2" column="leftProperty2" type="string" />
<one-to-one name="oneToOneRight" column="oneToOneRightID" class="com.qqlove.hibernate.pojo.OneToOneRight" cascade="all" outer-join="true" />
</class>
</hibernate-mapping>
/**********************************************************************************/
[OneToOneRight.hbm.xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "./hbm/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.qqlove.hibernate.pojo.OneToOneRight" table="OneToOneRight">
<id name="id" column="id" type="long">
<generator class="foreign">
<param name="property">oneToOneLeft</param>
</generator>
</id>
<property name="rightProperty1" column="rightProperty1" type="string" />
<property name="rightProperty2" column="rightProperty2" type="string" />
<one-to-one name="oneToOneLeft" cascade="all" class="com.qqlove.hibernate.pojo.OneToOneLeft" constrained="true">
</one-to-one>
</class>
</hibernate-mapping>
<one-to-one name="oneToOneLeft" column="id" cascade="all" class="com.qqlove.hibernate.pojo.OneToOneLeft" constrained="true">好象不能这么写吧.
要么在OneToOneLeft.hbm.xml下有
<one-to-one name="oneToOneRight" column="oneToOneRightID" class="com.qqlove.hibernate.pojo.OneToOneRight" cascade="all" outer-join="true" />
要么在OneToOneRight.hbm.xml下有
<one-to-one name="oneToOneLeft" column="id" cascade="all" class="com.qqlove.hibernate.pojo.OneToOneLeft" constrained="true">如果想双向关联需要用 property-ref 这个属性.
Hibernate doesn’t “manage” persistent associations. If you want to manipulate
an association, you must write exactly the same code you would write
without Hibernate. If an association is bidirectional, both sides of the relationship must be considered. Programming models like EJB entity beans
muddle this behavior by introducing container-managed relationships. The
container automatically changes the other side of a relationship if one
side is modified by the application. This is one of the reasons why code
that uses entity beans can’t be reused outside the container.