Search code examples
asp.netnhibernatenhibernate-mappingmapping

How to map Items which its SubItems are in the same Table with Nhibernate?


I am trying to build a messaging system and for this i have the table definition below

Message

Id
From
To
Body
ParentId // Subcollection, i want to get Asnwers (Message.ParentId== Message.Id)
IsRead

and i have this in the Message.cs

IList<Message> Answers;

I have tried this but it gives me all the messages and all the answers in the main collection.

But i dont want answers to be seen like a message (like the main item).

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="RealEstate.Core" namespace="RealEstate.Core.Domain">
  <class name="Message" table="Message" lazy="true">
    <id column="id" type="Int64" name="Id">
      <generator class="native" />
    </id>
    <property name="From" column="[From]" type="Int64" />
    <property name="To" column="[To]" type="Int64" />
    <property name="Body" column="Body" />
    <property name="ParentId" column="ParentId" type="Int64" />
    <property name="SenderType" column="SenderType" type="Byte" />
    <property name="IsRead" column="IsRead" type="Boolean" />

    <bag name="Answers" lazy="true" cascade="delete">
      <key column="ParentId" />
      <one-to-many class="Message"/>
    </bag>

  </class>
</hibernate-mapping>

How can this mapping be done, they are in the same table ?

Thank you very much


Solution

  • You want to map a tree ? Maybe this could help: how to map a tree in nhibernate