Question

I have one table names SchoolStricture in which the Primary key is "ID" and one another feild is "ParentStructureEntityID" which stores the primary key of the same table. The purpose of this table is to create a tree structure. During that parent's id will store in this "ParentStructureEntityID". I am a beginner in NHibernate. How can i map these two feild in my .hbm.xml file.

Please help me out of this.....

Was it helpful?

Solution

Your xml mapping will be something like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

  <class name="SchoolStructure" table="SchoolStructure">
    <id name="ID">
      <column name="ID"/>
      <generator class="native" />
    </id>
    <many-to-one name="ParentStructure" column="ParentStructureEntityID" />
  </class>
</hibernate-mapping>

And your class will be:

public class SchoolStructure
{
    public virtual int ID {get; private set;}
    public virtual SchoolStructure ParentStructure {  get; set;}
}

That's of course for starters. You should ideally override GetHashCode and Equals in proper manner. Finally I humbly suggest using mapping by code instead of xml mapping. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top