One-to-many mapping using the ref property not for the primary key

I have a problem that I cannot solve. Some of you may have encountered a similar problem.

I have two classes - Person and Contact and mapping for loading all contacts for a person. These examples are simplified for better reading.

Person.cs

public class Person
{
   public long Id { get; set; }
   public long ObjectId { get; set; }
   public IList<Contact> Contacts { get; set; }
}

Contact.cs

public class Contact
{
   public long Id { get; set; }
   public string Text { get; set; }
}

Person.hbm.xml

<property name="ObjectId" type="System.Int64" column="OBJECTID" />
<bag name="Contacts" lazy="true" cascade="all">
   <key column="PERSON_OBJECTID" property-ref="ObjectId" not-null="true" />
   <one-to-many class="Contact" />
</bag>

Contacts do not have mapping for the Person, they simply are not needed.

One person can have several contacts, and one contact can have only one owner. But I create versions of people when some of their data changes, because I need to keep a history of changes. The tables look like this:

PERSONAL TABLE

----------------------------------------
| ID | OBJECTID | FIRSTNAME | LASTNAME |
----------------------------------------
| 1  |    100   |   David   |   Gray   |
| 2  |    100   |   David   |   Gray   |
| 3  |    100   |   David   |   Gray   |
----------------------------------------

CONTACT TABLE

--------------------------------------------
| ID | PERSON_OBJECTID |        TEXT       |
--------------------------------------------
| 1  |       100       | email@email.com   |
| 2  |       100       | some phonenumber  |
--------------------------------------------

CONTACTS , PERSON - ObjectId , . , .

[ # 100] - role: Person.Contacts,

, #ObjectId, #Id. , NHibernate, ? property-ref . ( , Id objectId)?

, , - , - .

EDIT:

SQL select , , :

select c.* from persons p
inner join contacts c on c.person_objectid = p.objectid
where p.id = 1;

, ?:)

+4
1

NH property-ref, - , , - unique = "true" -ref.

Person.hbm.xml -

<property name="ObjectId" type="System.Int64" column="OBJECTID" unique="true"/>
+1

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


All Articles