Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

NHibernate, mapping an association to same table

I've an entity A which has an Id and ParentId column.

Currently I'm mapping it using a many-to-one relationship <many-to-one name="Parent" column="ParentID" />.

Parent property is of type A. In other words, Parent is an object of A.

Basically the problem is property "Parent" is always null, but this is incorrect since database has ParentID assigned with the right identifier.

What's wrong?

Thank you!

UPDATE

I got a hint. In fact, I've another entity which maps many instances of A.

If I get the other entity which has a collection of A, A entities have its Parent property unloaded. In the other hand, if I get an A by its Id, Parent gets loaded as expected.

It's something with the other mapping then:

<set name="Fields" table="A" cascade="all-delete-orphan" lazy="false">
  <key column="AID" />

  <one-to-many
    class="A"
  />
</set>

Upvotes: 0

Views: 419

Answers (1)

dumdum
dumdum

Reputation: 868

That should work. Make sure you have an foreign-key constraint on ParentId to Id, and you can try adding class="A, Assembly" to your many-to-one mapping to be explicit.

Upvotes: 1

Related Questions