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.  [ 13 posts ] 
Author Message
 Post subject: How To: HQL get parent obj if child obj exists in parent
PostPosted: Thu May 29, 2008 11:22 am 
Newbie

Joined: Sun May 04, 2008 1:10 pm
Posts: 8
Hibernate version: 1.4

Assumption: a child object exists in one of the parent object's list of children

HQL:
from Parent p where (from Child c where c.Id = :childID) in p.Children

Parent Object Model:
public Guid Id
public String Name
public IList<Child> Children

Child Object Model:
public Guid Id
public String Name

This currently returns an exception:
unindexed collection before [] [from Parent p where (from Child c where c.Id = :childID) in p.Children]

Some direction on how to do this properly would be appreciated.

**I have researched the forums and the nHibernate Documentation. Please include some terminology or concept names that would help me understand more completely the nuisances of CreateQuery method.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 3:15 am 
Newbie

Joined: Wed May 30, 2007 8:18 am
Posts: 12
Would something like following be helpful?

select c.Parent from Child c where ......


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 9:02 am 
Newbie

Joined: Sun May 04, 2008 1:10 pm
Posts: 8
pappukanghi wrote:
Would something like following be helpful?

select c.Parent from Child c where ......


Thanks for the reply, however Child does not have a property: Parent.

I would like to keep it that way to avoid circular references.

Brian


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 10:15 am 
Newbie

Joined: Wed May 30, 2007 8:18 am
Posts: 12
Then what field / property marks the relation between parent and child?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 10:23 am 
Newbie

Joined: Sun May 04, 2008 1:10 pm
Posts: 8
pappukanghi wrote:
Then what field / property marks the relation between parent and child?


Parent Object Model:
public Guid Id
public String Name
public IList<Child> Children


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 10:24 am 
Newbie

Joined: Wed May 30, 2007 8:18 am
Posts: 12
Yes but what determines on the child side as what parent a given child belongs to?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 10:55 am 
Newbie

Joined: Sun May 04, 2008 1:10 pm
Posts: 8
pappukanghi wrote:
Yes but what determines on the child side as what parent a given child belongs to?


HQL: Query the objects not the DB.

<class name="Parent" table="Parents">
<bag name="Children" cascade="all-delete-orphan" lazy="true" table="ParentChildren">
<key column="idParent"/>
<many-to-many class="Child, [assembly]" column="idChild"/>
</bag>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 10:59 am 
Newbie

Joined: Wed May 30, 2007 8:18 am
Posts: 12
Then, my friend as far as I know, nhibernate cannot help there. Every hql query gets converted into sql suitable for underlying database.

I could be wrong there (may not have explored nhibernate enough) but i have never seen such a thing (querying memory objects) in my experience with NH for last year and a half.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 11:13 am 
Newbie

Joined: Sun May 04, 2008 1:10 pm
Posts: 8
pappukanghi wrote:
Then, my friend as far as I know, nhibernate cannot help there. Every hql query gets converted into sql suitable for underlying database.

I could be wrong there (may not have explored nhibernate enough) but i have never seen such a thing (querying memory objects) in my experience with NH for last year and a half.


Let me understand, in your experience, nHibernate cannot create this Sql Query:

SELECT Parents.*

FROM Parents

WHERE Parents.idParent in

(SELECT ParentChildren.idParent
FROM ParentChildren
WHERE idChild = @childID)

?

Which returns (in SQL) the Parent Record.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 11:18 am 
Newbie

Joined: Wed May 30, 2007 8:18 am
Posts: 12
Yes it can but this is not what you posted originally. There was no mention of ParentChildren table in between.

What you have mentioned in your last post is do-able. But again everything must be in underlying database to run a query on.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 11:32 am 
Newbie

Joined: Sun May 04, 2008 1:10 pm
Posts: 8
pappukanghi wrote:
Yes it can but this is not what you posted originally. There was no mention of ParentChildren table in between.

What you have mentioned in your last post is do-able. But again everything must be in underlying database to run a query on.


By design, there was no database structure include in the original post because I'm querying objects not the db...and that's what I request help with.

'Doable'?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 1:01 pm 
Newbie

Joined: Wed May 30, 2007 8:18 am
Posts: 12
Objects only? I cannot help there. May be somebody with more experience with NH can.


Top
 Profile  
 
 Post subject: Re: How To: HQL get parent obj if child obj exists in parent
PostPosted: Fri May 30, 2008 1:01 pm 
Newbie

Joined: Sun May 04, 2008 1:10 pm
Posts: 8
bsausser wrote:
Hibernate version: 1.4
HQL:
from Parent p where (from Child c where c.Id = :childID) in p.Children


Solution: change HQL to use elements of Parent.Children.

HQL:
from Parent p where (from Child c where c.Id = :childID) in elements(p.Children)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.