2

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.

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>
2
  • 1
    I think you got the concepts mixed up. FABMatrixData should have a many-to-one to FABType, not one-to-one. And FABType should have none, or an inverse bag of FABMatrixData, not many-to-one. Commented Nov 15, 2010 at 3:21
  • Slightly irrelevant, but why dont you use Fluent NHibernate? It would be easier to map since it is validated at compile-time. All of the above can be easily mapped using FNH. If you need help on FNH let me know. Commented Jun 16, 2011 at 12:27

1 Answer 1

1

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>
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.