NHibernate bi-directional one-to-one mapping problem

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'. 
+4
source share
1 answer

There are two varieties of one-to-one association:

• primary key associations

• unique foreign key associations

Primary key associations do not need an additional column in the table; if two rows are associated by an association, then two rows of the table have the same primary key value. Therefore, if you want two objects to be associated with a primary key association, you must make sure that they are assigned the same identifier value! To associate primary keys, add the following mappings for Employee and Person, respectively.

 <one-to-one name="Person" class="Person"/> <one-to-one name="Employee" class="Employee" constrained="true"/> 

Now we need to make sure that the primary keys of the related rows in the PERSON and EMPLOYEE tables are equal.

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> 

The newly saved instance of Person is assigned the same primar key value as the Employee instance with the property of the employee of this Person. Alternatively, a foreign key with a unique constraint from Employee to Person can be expressed as:

 <many-to-one name="Person" class="Person" column="PERSON_ID" unique="true"/> 

And this association can be made bi-directional by adding the following to the Person mapping :

 <one-to-one name="Employee" class="Employee" property-ref="Person"/> 

Look at this

https://forum.hibernate.org/viewtopic.php?p=2362617&sid=23c4df33b683409df9b5d844037d6d03

+8
source

Source: https://habr.com/ru/post/1299715/


All Articles