NHibernate双向一对一映射问题 [英] NHibernate Bi-Directional one-to-one mapping problem
问题描述
当尝试在NHibernate中创建双向一对一映射时,我发现我无法递归获得对象的引用.
While trying to create a Bi-directional one-to-one mapping in NHibernate, I found that, I am unable to have the Reference of the objects recursively.
例如:假设我在Person
和Address
之间具有一对一的关系.
For example: suppose I have one-to-one relationships between Person
and Address
.
然后在执行以下代码后,
then after executing the following code,
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);
我需要从p.Address.Person
获得一个非null
值. IE. ID为1的同一个人.
I need to have a non-null
value from p.Address.Person
. I.e. the same person with an ID of 1.
但是该属性返回一个null
值.
But the property is returning a null
-value.
我应该寻找什么来解决这个问题?
What should I look for to solve the problem?
我的数据库表是这样的:
My database tables are like this:
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'.
推荐答案
一对一关联有两种:
•主键关联
•唯一的外键关联
主键关联不需要额外的表列;如果通过关联将两行相关联,则 两个表行共享相同的主键值.因此,如果您希望通过主键关联来关联两个对象, 您必须确保为它们分配了相同的标识符值! 对于主键关联,分别将以下映射添加到Employee和Person.
Primary key associations don't need an extra table column; if two rows are related by the association then the two table rows share the same primary key value. So if you want two objects to be related by a primary key association, you must make sure that they are assigned the same identifier value! For a primary key association, add the following mappings to Employee and Person, respectively.
<one-to-one name="Person" class="Person"/>
<one-to-one name="Employee" class="Employee" constrained="true"/>
现在,我们必须确保PERSON和EMPLOYEE表中相关行的主键相等.
Now we must ensure that the primary keys of related rows in the PERSON and EMPLOYEE tables are equal.
我们使用一种特殊的NHibernate标识符生成策略,称为外来策略:
We use a special NHibernate identifier generation strategy called foreign:
<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实例引用的相同的主键值 具有该人员的Employee属性. 另外,从雇员到人,具有唯一约束的外键可以表示为:
A newly saved instance of Person is then assigned the same primar key value as the Employee instance refered with the Employee property of that Person. Alternatively, a foreign key with a unique constraint, from Employee to Person, may be expressed as:
<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
这篇关于NHibernate双向一对一映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!