views:

1464

answers:

5

Hello,

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?

A: 

Seems like you'd just treat it the same as any other one-to-one relationship?

Spencer Ruport
Does that work if it can be null?
A: 

i think its a simple one-to-one.

i hope this will help u

<one-to-one
name="PropertyName"
class="ClassName"
cascade="all|none|save-update|delete"
constrained="true|false"
fetch="join|select"
property-ref="PropertyNameFromAssociatedClass"
access="field|property|nosetter|ClassName"/>
Chen Kinnrot
+1  A: 

Seems like you're willing to map a tree:

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx

Frederik Gheysels
Can NHibernate map only one many-to-one relationship? Usually you allways need both sites.
You can map the other end as well.You'll have to ends:a one-to-many which contains the children of the current nodea one-to-one which refers to the parent of the current node
Frederik Gheysels
+1  A: 

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" />
Stefan Steinegger
A: 

How do you manage Delete this row?

Murali