NHibernate双向一对一映射问题

时间:2010-01-29 06:37:38

标签: nhibernate bidirectional-relation

在尝试在NHibernate中创建双向一对一映射时,我发现,我无法递归地获取对象的引用。

例如:假设我在PersonAddress之间建立了一对一的关系。

然后执行以下代码后,

class Person
{
    ... ...
    public Address Address { get;set; }
}

class Address
{
    ... ...
    public Person Person {get;set;}
}

Repository<Person> rep = new Repository<Person>();
Person p = rep.Get<Person>(1);

我需要null的非p.Address.Person值。即身份证号为1的同一个人。

但该属性返回null - 值。

我应该寻找什么来解决问题?

我的数据库表是这样的:

Address {ID, Desc}
Person {ID, Name, AddressID}

Person.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping
    xmlns="urn:nhibernate-mapping-2.2"
     default-access="property"
    >
  <class name="NHibernate__BiDirectional__One_To_One.BO.Person, NHibernate__BiDirectional__One_To_One.BO" 
         table="Person">
    <id name="ID">
      <generator class="native" />
    </id>
    <property name="Name"/>

    <many-to-one
        name="Address"
        class="NHibernate__BiDirectional__One_To_One.BO.Address, NHibernate__BiDirectional__One_To_One.BO"
        column="AddressID" 
        cascade="all" 
        unique="true" />

  </class>
</hibernate-mapping>

Address.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping
  xmlns="urn:nhibernate-mapping-2.2"
   default-access="property"
  >
  <class name="NHibernate__BiDirectional__One_To_One.BO.Address, NHibernate__BiDirectional__One_To_One.BO" 
         table="Address">
    <id name="ID" >
      <generator class="native" />
    </id>
    <property name="Desc"/>      
    <one-to-one
        name="Person"
        class="NHibernate__BiDirectional__One_To_One.BO.Person, NHibernate__BiDirectional__One_To_One.BO"
        />
  </class>
</hibernate-mapping>

我也遇到了错误:

could not load an entity: [NHibernate__BiDirectional__One_To_One.BO.Person#1][SQ
L: SELECT person0_.ID as ID0_1_, person0_.Name as Name0_1_, address1_.ID as ID1_
0_, address1_.Desc as Desc1_0_, address1_.AddressID as AddressID1_0_ FROM Person
 person0_ left outer join Address address1_ on person0_.ID=address1_.AddressID W
HERE person0_.ID=?]
Incorrect syntax near the keyword 'Desc'.

1 个答案:

答案 0 :(得分:8)

有两种一对一的关联:

•主键关联

•唯一的外键关联

主键关联不需要额外的表列;如果两行通过关联相关,那么 两个表行共享相同的主键值。因此,如果您希望通过主键关联将两个对象相关联, 您必须确保为它们分配了相同的标识符值! 对于主键关联,分别将以下映射添加到Employee和Person。

<one-to-one name="Person" class="Person"/>
<one-to-one name="Employee" class="Employee" constrained="true"/>

现在我们必须确保PERSON和EMPLOYEE表中相关行的主键相等。

我们使用一种名为foreign的特殊NHibernate标识符生成策略:

<class name="Person" table="PERSON">
<id name="Id" column="PERSON_ID">
<generator class="foreign">
<param name="property">Employee</param>
</generator>
</id>
...
<one-to-one name="Employee"
class="Employee"
constrained="true"/>
</class>

然后为新保存的Person实例分配与引用的Employee实例相同的primar键值 使用该Person的Employee属性。 或者,具有唯一约束的外键(从Employee到Person)可表示为:

<many-to-one name="Person" class="Person" column="PERSON_ID" unique="true"/>

通过在Person映射中添加以下内容,可以使此关联成为双向

<one-to-one name="Employee" class="Employee" property-ref="Person"/>

看看这个

https://forum.hibernate.org/viewtopic.php?p=2362617&sid=23c4df33b683409df9b5d844037d6d03