gpt4 book ai didi

NHibernate 双向一对一映射问题

转载 作者:行者123 更新时间:2023-12-03 12:03:56 25 4
gpt4 key购买 nike

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

例如:假设我在 Person 之间有一对一的关系和 Address .

然后在执行以下代码后,

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 . IE。 ID为1的同一个人。

但该属性(property)返回 null -值(value)。

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

我的数据库表是这样的:
Address {ID, Desc}
Person {ID, Name, AddressID}

人员.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>

地址.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。

<one-to-one name="Person" class="Person"/>
<one-to-one name="Employee" class="Employee" constrained="true"/>
现在我们必须确保 PERSON 和 EMPLOYEE 表中相关行的主键相等。
我们使用一种特殊的 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 实例相同的主键值
使用该 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"/>
来源: Chapter 5. Basic O/R Mapping - 5.1.12. one-to-one
看看这个
Hibernate Community • View topic - one-to-one with foreign key on child table.

关于NHibernate 双向一对一映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2160324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com