views:

128

answers:

2

I googled all day and I cant find one good example how to map this kind of objects:

class Parent{
    private Integer parentId;
    private String parentName;
    private List<Child> childs;

    // ....... getters and setters ............
}

class Child{
    private Integer childId;
    private String childName;

    private Parent parent;

    // ....... getters and setters ...........
}

I have no idea how to make map for this kind of List.

A: 

First of all, you should declare your List<Child> as IList<Child>, because NHibernate needs to be able to use its own collection-type that implements IList.

In your mapping, you should use the 'bag' element to map your List. (Are you sure you want to use a List and not a Set ? Since a List allows that one single entity occurs more then once in the list, whereas a Set does not allow that).

This is how I should do it:

public class Parent
{
   private IList<Child> _children = new List<Child>();

   public ReadOnlyCollection<Child> Children
   {
       get {return _children.AsReadOnly();}
   }
}

And the mapping:

<class name="Parent" table="Parent">
    <list name="Children" table="..." access="field.camelcase-underscore" inverse="true">
        <key column="..." />
        <one-to-many class="Child" />
    </list>
</class>

(Ommitted al the other properties for brevity)

(Ow, now that I see it, you're using Hibernate ? My code example is in C#, so I do not know whether you have the notion of ReadOnlyCollection etc...)

Frederik Gheysels
Since the question is tagged "hibernate" I assume he is using Java and not C#, so "List<Child>" is correct (in Java List is the interface, the implementations are ArrayList, LinkedList, ...).
Joachim Sauer
+1  A: 

Hibernate documentation has plenty of examples, including this one which is basically what you're looking for. XML mapping would for your case would look like:

<class name="Parent" table="Parent">
  <id name="parentId" column="id" type="integer" /> <!-- TODO: specify generator -->
  <property name="parentName" type="string" column="name" />
  <bag name="childs" table="Children" inverse="true">
    <key column="parent_id" />
    <one-to-many class="Child" />
  </bag>
</class>

<class name="Child" table="Children">
  <id name="childId" column="id" type="integer" /> <!-- TODO: specify generator -->
  <property name="childName" type="string" column="name" />
  <many-to-one name="parent" column="parent_id" not-null="true"/>
</class>

For examples of annotation-based mapping take a look here

ChssPly76
Doesn't that map the List as a mere Collection (i.e. not keeping the order of the List intact)?
Joachim Sauer
It's mapped as bag - keeping list semantics, but not maintaining order. If order is important (OP didn't mention that), `<bag>` can be replaced with `<list>` but you'll need another column in the `Children` table to maintain element indexes.
ChssPly76