NHibernate, collection with composite-id.

Thursday, March 19, 2009 5:59 PM | Leave a reply »

In the previous post we saw how to map an entity with a composite-id.

Well but now if you have mapped an entity in this way and you need to create a collection of this entity or maybe you have a related child class that uses this primary keys … you are in trouble! Smile

The way to solve the problem is very easy. First of a short example to understand what I’m talking about:

   1: class Foo {
   2:     public string name { get; set; }
   3:     public string lastname { get; set; }
   4:     public IList<childfoo> listofchild { get; set; }
   5: }
   6: class childfoo {
   7:     public string name { get; set; }
   8:     public string lastname { get; set; }
   9:     public Foo father { get; set; }
  10: }

Now the first class will be mapped in this way if we assume that “name and lastname are the primary keys”. (Also remember to override Equals() and GetHashCode()!!).

   1: <class name="Foo" table="[Foo]" schema="xxx">
   2:   <composite-id>
   3:     <key-property name="name">
   4:       <column name="[name]" sql-type="varchar" length="50" not-null="true"/>
   5:     </key-property>
   6:     <key-property name="lastname">
   7:       <column name="[lastname]" sql-type="varchar" length="50" not-null="true"/>
   8:     </key-property>
   9:   </composite-id>
   1: <bag name="listofchild" table="[childfoo]" inverse="true"
   2:    cascade="all-delete-orphan" generic="true" lazy="true">
   3:   <key>
   4:     <column name="[name]" sql-type="varchar"/>
   5:     <column name="[lastname]" sql-type="varchar"/>
   6:   </key>
   7:   <one-to-many class="Foo" not-found="ignore"/>
   8: </bag>

Now we can go back to the child mapping file and reference the many-to-one foreign mapping in this simple way:

   1: <many-to-one name="Foo" lazy="proxy" not-null="false">
   2:   <column name="[name]" sql-type="varchar"/>
   3:   <column name="[lastname]" sql-type="varchar"/>
   4: </many-to-one>

So now my repository will be able to give me:

image

And in that moment my repository will go back and execute the sub-select.


Comments

  1. Re : # health benefits of acai berry

    The announcement undoubtedly raises some eyebrows. The elder Murtha is...
  2. Gravatar Pedro Dias says:

    Re : # re: NHibernate, collection with composite-id.

    I'm having a huge headache trying to solve a similar situation:

    public class Task
    {
    public int Id{ get; set; }
    public IList< TaskAttribute > Attributes{ get; set; }
    }

    public class TaskAttribute
    {
    public int TaskId{ get; set; }
    public string TaskName{ get; set; }
    public string Value{ get; set; }
    }


    This is a fairly simple object structure, but I cannot, for the life of me, figure out how to model this in hbm. The problem comes when I try to persist the objects, since TaskAttributes is a compound key that maps both to the foreign Task table as well as its own Name to make one TaskAttribute unique.

    HBM:





    Would you have any light to shed on this?
  3. Gravatar raffaeu says:

    Re : # re: NHibernate, collection with composite-id.

    I will have a look this week-end and let you know.
    What type of error do you receive?
Comments have been closed on this topic.