-->
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: Collection subset match query
PostPosted: Thu Mar 09, 2006 7:36 pm 
Newbie

Joined: Wed Mar 08, 2006 5:59 pm
Posts: 2
Location: Los Angeles
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Name and version of the database you are using: 0racle 10

I am attempting to write queries against tree-structured data. Parent and child objects are of the same type (stored in the same table, and mapped through a getParents() collection. The parent-child relationship is many to many.

My need is to write an HQL query which returns a collection of these values which have all of a given set of parents, but possibly more parents than those in the set. So for a set

Code:
Set<Value> required;


I need to write HQL that efficiently returns all values which have at least all those required parents.

Currently, I am generating dynamic HQL by iterating over the set of required parents and generating an in clause for each of them, e.g. for a two-required-parent case

Code:
  from Values v
    where ? in elements(v.parents)
      and ? in elements(v.parents)


with the two values from the required set passes as arguments. But this produces nasty repeated selects for the parents in the generated SQL, and seems wrong just on the esthetic level.

Is there a better way to do a collection-subset query like this?


Top
 Profile  
 
 Post subject: This may work...
PostPosted: Thu Mar 09, 2006 11:23 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
I am assuming that parent to value is many-to-many, and that there is a mapped Java class representing this relation called ValueToParent, with two fields called "valueRef" and "parentRef". Then I would try doing something like this:

Code:
select
    valueRef
from
    ValueToParent
where
    parentRef.id in (:parent_ids)
group by
    valueRef
having
    count(valueRef) = :num_of_parents


Bind :parent_ids to a collection of parent IDs. Bind :num_of_parents to the number of distinct values in that collection. The query should get you only the values with all parents from your query.

I am not 100% certain on how fast this thing is going to be (and even if HQL parser will take it), but there is a good chance that it might help.

Please give it a try and let me know if it helped :-)


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.