Community discussion forum

Nhibernate mapping prob in a c# application

  • 2 years ago

    Hi All !!

        I have a problem with mapping a one to many and many to many table structure for a NHibernate xml mapping file. I have tried sevaral samples on the web, but non of them worked for me. If someone can provide me a very clear and simple sample to how to do this, that would be really greatfull.

    Thanks 

     

  • 2 years ago

    for many to many relationships we have used code like this: 

     

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
     <class name="myNamespace.myObject, myAssembley" table="myTable">
      <id name="Id" column="myId" type="Int64">
       <generator class="assigned" />
      </id>

      <property name="MyDate" column="myDate" type="DateTime" />

      <set name="MediaList" table="myMediaList" outer-join="false" cascade="none" inverse="true" >
       <key column="myId"  />
       <many-to-many class="myNamespace.Media, myAssembley" column="mediaId"  foreign-key="mediaId"/>
      </set>
     </class>
    </hibernate-mapping>

  • 2 years ago

    And for the one to many relationships: 

    ....

         <property name="MyProperty" column="myProperty" type="Int32" />
       

        <bag name="Employees" inverse="true" order-by="rangOrde">
          <key column="officeNumber"/>
          <one-to-many class="myNamespace.MyEmployee, MyAssembley"/>
        </bag>

     </class>
    </hibernate-mapping>

Post a reply

Enter your message below

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!