I have a parent/child association. Can I get parent with children collection, containing some part of all parent's children using some HQL/Criteria query?
for example:
class Parent {
Long id;
String name;
Collection children;
...
}
class Child {
Long id;
String name;
Parent parent;
...
}
PARENT_TABLE
ID | NAME
=============
1 | parent1
2 | parent2
CHILDREN_TABLE
ID | NAME | PARENT_ID
======================
1 | a1 | 1
2 | b1 | 1
3 | a2 | 2
Can I get parent list, where children collections contains only children, whoose name starts with 'a', so that:
List parents = someQuery.list();
for (Iterator iter = parents.iterator(); iter.hasNext(); ) {
Parent parent = (Parent) iter.next();
for (Iterator ch = parent.getChildren().iterator();
ch.hasNext(); ) {
Child child = (Child) ch.next();
assertTrue(child.getName().startsWith("a"));
}
}
Please, help.
-
Hibernate: fetching part of collection (9 messages)
- Posted by: Yaroslav Gnatyuk
- Posted on: April 14 2005 07:17 EDT
Threaded Messages (9)
- criteria query by Vinod Singh on April 14 2005 09:44 EDT
- criteria query by Yaroslav Gnatyuk on April 14 2005 12:39 EDT
- criteria query by Matthew Wessel on April 18 2005 09:03 EDT
- criteria query by Yaroslav Gnatyuk on April 14 2005 12:39 EDT
- Criteria Query by Reza Rahimi on April 19 2005 06:46 EDT
- Criteria Query by Reza Rahimi on April 19 2005 06:54 EDT
- HQL Sample by Reza Rahimi on April 19 2005 07:35 EDT
- HQL Sample by Yaroslav Gnatyuk on April 26 2005 15:22 EDT
-
HQL Sample by Reza Rahimi on May 01 2005 06:02 EDT
- HQL Sample by Yaroslav Gnatyuk on May 21 2005 02:47 EDT
-
HQL Sample by Reza Rahimi on May 01 2005 06:02 EDT
- HQL Sample by Yaroslav Gnatyuk on April 26 2005 15:22 EDT
-
criteria query[ Go to top ]
- Posted by: Vinod Singh
- Posted on: April 14 2005 09:44 EDT
- in response to Yaroslav Gnatyuk
Try Criteria query -
criteria query[ Go to top ]
- Posted by: Yaroslav Gnatyuk
- Posted on: April 14 2005 12:39 EDT
- in response to Vinod Singh
But I don't know, what query to use! I tried some Criteria queries and some HQL queries, but I still can't find the solution.
Please, if you know, how to do this, write it.
Many thanks. -
criteria query[ Go to top ]
- Posted by: Matthew Wessel
- Posted on: April 18 2005 21:03 EDT
- in response to Yaroslav Gnatyuk
Am having a similar problem and am yet to find a suitable solution. Any assistance would be much appreciated! -
Criteria Query[ Go to top ]
- Posted by: Reza Rahimi
- Posted on: April 19 2005 06:46 EDT
- in response to Yaroslav Gnatyuk
I used something like following in spring&hibernate&oracle environment and it worked properly.
.....
String queryText = "Select prnt FROM " + Parent.Class.getName() + " prnt " + " where prnt in (SELECT chl.parent from " + Child.class.getName() + " chl where chl.name like 'a%' ")
try
{
Query query = getHibernateTemplate().createQuery(getSession(), queryText);
List result = query.list();
...
}
catch( HibernateException he )
{
.....
} -
Criteria Query[ Go to top ]
- Posted by: Reza Rahimi
- Posted on: April 19 2005 06:54 EDT
- in response to Yaroslav Gnatyuk
I used something like following in spring&hibernate&oracle environment and it worked properly.
.....
String queryText = "Select prnt FROM " + Parent.Class.getName() + " prnt " + " where prnt in (SELECT chl.parent from " + Child.class.getName() + " chl where chl.name like 'a%' ");
try
{
Query query = getHibernateTemplate().createQuery(getSession(), queryText);
List result = query.list();
...
}
catch( HibernateException he )
{
.....
} -
HQL Sample[ Go to top ]
- Posted by: Reza Rahimi
- Posted on: April 19 2005 07:35 EDT
- in response to Yaroslav Gnatyuk
from Parent prnt where prnt in (SELECT chl.parent from Child chl where chl.name like 'a%') -
HQL Sample[ Go to top ]
- Posted by: Yaroslav Gnatyuk
- Posted on: April 26 2005 15:22 EDT
- in response to Reza Rahimi
Only one question here: if I'll take any parent from the resulting list, will it contain only children, that starts with 'a' (that's what I want), or it will contain all of it's children? -
HQL Sample[ Go to top ]
- Posted by: Reza Rahimi
- Posted on: May 01 2005 06:02 EDT
- in response to Yaroslav Gnatyuk
It will contain all of it's children because getChildren() of parent has its own fetching strategy based on hbm file definition . This strategy is not related to fetching method of parent . -
HQL Sample[ Go to top ]
- Posted by: Yaroslav Gnatyuk
- Posted on: May 21 2005 14:47 EDT
- in response to Reza Rahimi
Oh yes, just as I thought. But this code doesnt solve the problem so far. Why then post? And I find separate mapping for each special case of fetching too expensive.