Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a self-reference in NHibernate

I have a very special NHibernate mapping case. The class has a reference to itself.

public class MyClass
{
   public Guid Id { get; set; }
   public MyClass SelfReference { get; set; }
}

The data base table has a foreign key field on the primary key of the same table. And event worse, this self reference can be null.

Is that possible to map and how can this be done?


2 Answers

one-to-one can be used to synchronize primary keys and is used rarely, in my experience many-to-one is the most "natural" association for "normal" references:

<many-to-one name="SelfReference" class="MyClass" column="SelfReference_FK" />
like image 149
Stefan Steinegger Avatar answered Feb 04 '26 05:02

Stefan Steinegger


Seems like you're willing to map a tree:

http://nhibernate.hibernatingrhinos.com/16/how-to-map-a-tree-in-nhibernate

like image 30
Frederik Gheysels Avatar answered Feb 04 '26 06:02

Frederik Gheysels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!