-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: One-to-many collection containing null values
PostPosted: Fri May 04, 2007 6:43 am 
Newbie

Joined: Fri May 04, 2007 4:59 am
Posts: 2
Hi!

I am new to NHibernate, so my question may be trivial. I have Parent-Child relationship (as one-to-many). I am trying to fetch all Parent objects with their Children. Currently, I have two parents in database, one with single child, the other with two children.

NHibernate version: 1.0.4.0

Mapping documents:
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
    <class name="BusinessEntity.Parent, BusinessEntity" table="Parent">
    <id name="Id" column="ParentId" type="Int32" unsaved-value="0">
    <generator class="assigned" />
    </id>
    <property name="Name"/>
    <property name="Gender"/>

    <list name="Children" fetch="select">
    <key column ="ParentId" />
    <index column="ChildId" />
    <one-to-many class="BusinessEntity.Child, BusinessEntity"/>
    </list>
    </class>
    </hibernate-mapping>

    <class name="BusinessEntity.Child, BusinessEntity" table="Child">
    <id name="Id" column="ChildId" type="Int32" unsaved-value="0">
    <generator class="assigned" />
    </id>
    <property name="Name"/>
    <property name="Age"/>
    <property name="ParentId"/>
    </class>


Both Parent and child implement matching interfaces:
Code:
public interface IParent
{
   int Id
   {
      get;
      set;
   }

   string Name
   {
      get;
      set;
   }

   char Gender
   {
      get;
      set;
   }

   IList Children
   {
      get;
      set;
   }
}

public interface IChild
{
   int Id
   {
      get;
      set;
   }

   string Name
   {
      get;
      set;
   }

   ushort Age
   {
      get;
      set;
   }

   int ParentId
   {
      get;
      set;
   }
}


Code between sessionFactory.openSession() and session.close():
Code:
tx = session.BeginTransaction();
ICriteria criteria = session.CreateCriteria(typeof(Parent));
parents = criteria.List();
session.Close();


I fetch parents and first has collection of 4 children (with two NULL values) and the other 3 children (with two NULL values).

What seems to be the problem? I know I could use set (and ISet) instead of list (and IList), but I am trying to make it work with System.Collection namespace.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 04, 2007 11:05 am 
Newbie

Joined: Mon Apr 02, 2007 12:31 pm
Posts: 19
This is because you're using the ChildId as an index into the list. NHibernate follows list semantics, so if one parent has one child with ID=2, then that object will be loaded into parentObj.Children[2]. Since there are no children with ID's 0 or 1, parentObj.Children[0] and parentObj.Children[1] will be null.

If you really want to use a list, you should add another column to serve as the list index. After you add the property and map it, you won't need to worry about it again. You also may be able to use a Bag, which is implemented as an IList.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.