-->
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.  [ 5 posts ] 
Author Message
 Post subject: always empty collection of child objects
PostPosted: Wed Sep 14, 2005 10:35 am 
Hello,

it seems to me that NHibernate doesn't want return the collection of child objects that are in the same class hierarchy as the parent.

In the following example I have base class QuestionBase and 2 derived classes Question & Group.
Object Group contains collection of Question objects.

I have created one group in the DB and inserted a few Questions in the Group.
But when I retrieve this Group via NHibernate and look at it's Questions collection, it is always empty.

What is the reason?

Mapping file:
Code:
<class name="QuestionBase" table="Question" discriminator-value="b">
  <id name="Id" type="Int32" column="QuestionID" unsaved-value="0">
   <generator class="identity" />
  </id>
  <discriminator column="Type" type="Char" />
  <property name="Text" type="String" not-null="true" />

  <subclass name="Question" discriminator-value="q">
    <property name="Name" type="String" length="10" />
   <property name="Mandatory" type="Boolean"  />
   <many-to-one name="Group" column="GroupID" class="Group" />
  </subclass> 

  <subclass name="Group" discriminator-value="g">
     <set name="Questions" inverse="true" lazy="true">
       <key column="QuestionID" />
        <one-to-many class="Question" />
      </set>
  </subclass>

</class>


Top
  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 11:29 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Your mapping is a bit strange, you have Group.Id and the key for Group.Questions mapped to the same column. You probably want the collection key to be GroupID.


Top
 Profile  
 
 Post subject: 2sergey
PostPosted: Wed Sep 14, 2005 12:12 pm 
Sergey, thank you for the help!

But I'm not sure that the key of the collection should be GroupID.

I have base class QuestionBase. It's identifier is QuestionID.
The same identifier have derived classes Question & Group.

I want retrieve all Questions in the Group. For this purpose
I use collection Questions in subclass Group with key QuestionID
(the id of Group).

And GroupID is database field (foreign key) of Question objects.

Please let me know if I'm not right.

Maybe the collection is empty because GROUP - is reserved word in SQL?

But I don't see an error on the page.

Best regards,
Sergey





> Your mapping is a bit strange, you have Group.Id and the key for
> Group.Questions mapped to the same column. You probably want the
> collection key to be GroupID.


Top
  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 1:58 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
How do you select the questions that belong to the group with ID 1? Do you do 'select * from Question where QuestionID = 1' or do you do 'select * from Question where GroupID = 1'? The field that you use to "identify" the collection (QuestionID or GroupID) is called collection key in NHibernate, and that's what the <key> element should map to.


Top
 Profile  
 
 Post subject: Thank you !!! I understood
PostPosted: Fri Sep 16, 2005 12:23 pm 
sergey wrote:
How do you select the questions that belong to the group with ID 1? Do you do 'select * from Question where QuestionID = 1' or do you do 'select * from Question where GroupID = 1'? The field that you use to "identify" the collection (QuestionID or GroupID) is called collection key in NHibernate, and that's what the <key> element should map to.


Top
  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.