When I tried to create bidirectional one-to-one mapping in NHibernate, I found that I could not recursively refer to objects.
For example: suppose I have a one-to-one relationship between Person and Address .
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 be null from p.Address.Person . That is, the same person with identifier 1.
But the property returns null .
What should I look for to solve a problem?
My database tables are as follows:
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 also get 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'.