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.  [ 4 posts ] 
Author Message
 Post subject: Lost: no idea how to retrieve lazy sets in HQL
PostPosted: Mon Sep 25, 2006 5:34 am 
Beginner
Beginner

Joined: Mon May 22, 2006 12:12 am
Posts: 23
Hi,


using NHibernate with C#.

Stupid, I still can't figure out the use of "lazy".

Just take my class, Document, as example.
An Document instance can be parent of many Document instances; similarly, it can children of many parents. In short, it is self-referencing.
Document class has id field called docId.
It uses ISet to define the parentDocs, childDocs collections.

To support the many-to-many structure, I've used a table named DocInDocuments to store the parent-child relationship.

Problem:

How do I retrieve the child (parent) document instances of a given document in code? I have difficulty writing the HQL (IQuery) statement to fetch appropriately. Anybody can guide me the proper syntax? Let say, I want to retrieve all childDocs of a Document instance where its docId is 1?


Pls find below my Document.hbm.xml on childDocs and parentDocs set:

<set name="childDocs"
inverse="true"
lazy="true"
cascade="all-delete-orphan" table="DocInDocuments">
<key column="parentDoc" foreign-key="fkChildDocs" />
<many-to-many class="Business.Document, Business" outer-join="auto">
<column name="childDoc"/>
</many-to-many>
</set>
<set name="parentDocs"
inverse="false"
cascade="all-delete-orphan" table="DocInDocuments">
<key column="childDoc" foreign-key="fkParentDocs" />
<many-to-many class="Business.Document, Business" outer-join="auto">
<column name="parentDoc"/>
</many-to-many>
</set>


thanks in advance!
jackLing


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 5:21 pm 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
I'm not very versed with HQL, but what about:

Code:
from Document parent left join fetch parent.childDocuments children
where parent.DocumentId = :document_id


..something like that.

EDIT: re-reading your post made me think of this:

Code:
from Document doc where doc.ParentDocument.Id= :parent_doc_id


and lazy doesn't really matter here. Your collection will by hydrated if necessary (you can specify "inner join fetch", for example)

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 11:52 pm 
Beginner
Beginner

Joined: Mon May 22, 2006 12:12 am
Posts: 23
Hi,


Thanks for the reply!
But I still in misery....

If I were to use:
" from Document parent inner join fetch parent.childDocs children where parent.docId = 2"

it seems that the returned object is from the parent. But in fact, I want to retrieve all children documents (not the parent's info)


If I use HQL with "select chidren" in front (i.e. "select children from Document parent inner join fetch parent.childDocs children where parent.docId = 2";), I have problem getting the object array result (note: system prompts that need to use object array, Object[] in the following code). But then I find that the object array has zero length (i.e. objs[0] is invalid array index)...

Hope someone can give insights...! Thanks in advance!


sQuery = "select children from Document parent inner join fetch parent.childDocs children where parent.docId = 2";
IQuery q = sess.CreateQuery(sQuery);


for (IEnumerator ie = q.List().GetEnumerator(); ie.MoveNext(); )
{
Object[] objs = (Object[])ie.Current;
doc = (Document)objs[0];
System.Windows.Forms.MessageBox.Show(doc.name);

alResult.Add(doc);
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 26, 2006 12:04 am 
Regular
Regular

Joined: Tue May 31, 2005 3:18 pm
Posts: 117
Location: Houston
i was reading in HIA today that if you structure your query WITHOUT the fetch, the query will actually return a 2-element array, the 0th index being your first Entity in the HQL query, and the 1st index being the 2nd....

(I may be mistaking this for the left join queries... where the first element might not have any of the 2nd element... thus a zero length array.... anyway if you have HIA it will really help here. )

_________________
------------------------------
Ben Scheirman
http://www.flux88.com


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