use*_*312 4 nhibernate bidirectional-relation
在尝试在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);
Run Code Online (Sandbox Code Playgroud)
我需要有一个非null值p.Address.Person.即ID为1的同一个人.
但该物业正在返还一个价值null.
我应该寻找什么来解决这个问题?
我的数据库表是这样的:
Address {ID, Desc}
Person {ID, Name, AddressID}
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)
我也收到一个错误:
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'.
Run Code Online (Sandbox Code Playgroud)
一对一关联有两种:
•主要关键协会
•独特的外键关联
主键关联不需要额外的表列; 如果关联的两行相关,则两个表行共享相同的主键值.因此,如果您希望通过主键关联关联两个对象,则必须确保为它们分配了相同的标识符值!对于主键关联,分别将以下映射添加到Employee和Person.
<one-to-one name="Person" class="Person"/>
<one-to-one name="Employee" class="Employee" constrained="true"/>
Run Code Online (Sandbox Code Playgroud)
现在我们必须确保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>
Run Code Online (Sandbox Code Playgroud)
然后为新保存的Person实例分配与Employee实例引用的该Person的Employee属性相同的primar键值.或者,具有唯一约束的外键(从Employee到Person)可表示为:
<many-to-one name="Person" class="Person" column="PERSON_ID" unique="true"/>
Run Code Online (Sandbox Code Playgroud)
并且可以通过将以下内容添加到Person映射来使此关联成为双向:
<one-to-one name="Employee" class="Employee" property-ref="Person"/>
Run Code Online (Sandbox Code Playgroud)
看看这个
https://forum.hibernate.org/viewtopic.php?p=2362617&sid=23c4df33b683409df9b5d844037d6d03
| 归档时间: |
|
| 查看次数: |
4447 次 |
| 最近记录: |