将DB结构映射到nHibernate映射文件

时间:2010-11-15 01:39:38

标签: nhibernate nhibernate-mapping

我有以下数据库结构,我需要创建相关的nHibernate映射文件。我遇到的问题是一对一,多对一和包映射。我目前的地图数据也在下面,任何帮助都可以理解。

alt text

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 class="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>

1 个答案:

答案 0 :(得分:1)

您需要执行以下步骤:

  1. 向FABMatrix_to_FABMatrixData表添加唯一ID。
  2. 将类型为FabMatrixData属性的ICollection添加到FABMatrix,object(“FABMatrixDataSet”)。
  3. 在FABMatrix承包商中将ICollection初始化为HashedSet -

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

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