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.
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:
- Add an unique ID to FABMatrix_to_FABMatrixData table.
- Add an ICollection of type FabMatrixData property to FABMatrix, object("FABMatrixDataSet").
Initialize your ICollection as a HashedSet in FABMatrix contractor -
class FABMatrix { public virtual ICollection<FabMatrixData> FABMatrixDataSet { get; set;} public FABMatrix() { FABMatrixDataSet = new HashedSet<FabMatrixData>(); } }
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>
精彩评论