开发者

Mapping DB structure to nHibernate mapping files

开发者 https://www.devze.com 2023-01-24 21:20 出处:网络
I have the following DB structure, and I need to create the relevant nHibernate Mapping files. The problem I am having is one-one, many-one and bag mappings. My current mapping data is also below, any

I have the following DB structure, and I need to create the relevant nHibernate Mapping files. The problem I am having is one-one, many-one and bag mappings. My current mapping data is also below, any help is appreciated to figure it out.

Mapping DB structure to nHibernate mapping files

FABMatrix

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="FABMatrix" table="FABMatrix" lazy="true">
        <cache usage="nonstrict-read-write"/>
        <id name="id" column="ID">
            <generator class="identity"/>
        </id>
        <property name="Name" column="ProductName"/>
        <bag name="FABData" table="FABMatrix_to_FABMatrixData">
            <key column="FABMatrixId"/>
            <many-to-many cla开发者_运维百科ss="FABMatrixData" column="FABDataId"/>
        </bag>
        <property name="DateCreated" column="DateCreated"/>
        <property name="DateModified" column="DateModified"/>
    </class>
</hibernate-mapping>

FABMatrixData

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="FabMatrixData" table="FABMatrixData" lazy="true">
        <cache usage="nonstrict-read-write"/>
        <id name="id" column="ID">
            <generator class="identity"/>
        </id>
        <property name="Text" column="Text"/>
        <one-to-one name="Type" class="FABType"></one-to-one>
        <property name="DateCreated" column="DateCreated"/>
        <property name="DateModified" column="DateModified"/>
    </class>
</hibernate-mapping>

FABType

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="FABType" table="FABTypes" lazy="true">
        <cache usage="nonstrict-read-write"/>
        <id name="id" column="ID">
            <generator class="identity"/>
        </id>
        <property name="Name" column="Name"/>
        <many-to-one name="Data" class="FABMatrixData" column="FABTypeId">
        </many-to-one>
        <property name="DateCreated" column="DateCreated"/>
        <property name="DateModified" column="DateModified"/>
    </class>
</hibernate-mapping>


You need to do the following steps:

  1. Add an unique ID to FABMatrix_to_FABMatrixData table.
  2. Add an ICollection of type FabMatrixData property to FABMatrix, object("FABMatrixDataSet").
  3. Initialize your ICollection as a HashedSet in FABMatrix contractor -

    class FABMatrix
    {        
      public virtual ICollection<FabMatrixData> FABMatrixDataSet { get; set;}
      public FABMatrix()
      {
         FABMatrixDataSet = new HashedSet<FabMatrixData>();
      }
    }
    
  4. Map it with Set mapping -

    <set name="FABMatrixDataSet" table="FABMatrix_to_FABMatrixData" cascade="none">
        <key column="FABMatrixId"/>
        <many-to-many class="FABMatrixData" column="FABDataId"/>
    </set>
    
0

精彩评论

暂无评论...
验证码 换一张
取 消