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.
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);
I need to have a non-null
value from p.Address.Person
. I.e. the same person with an ID of 1.
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>
I am also getting an error:
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'.