Hibernate: fetching part of collection

Discussions

General J2EE: Hibernate: fetching part of collection

  1. Hibernate: fetching part of collection (9 messages)

    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.

    Threaded Messages (9)

  2. criteria query[ Go to top ]

    Try Criteria query
  3. criteria query[ Go to top ]

    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.
  4. criteria query[ Go to top ]

    Am having a similar problem and am yet to find a suitable solution. Any assistance would be much appreciated!
  5. Criteria Query[ Go to top ]

    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 )
    {
    .....
    }
  6. Criteria Query[ Go to top ]

    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 )
    {
    .....
    }
  7. HQL Sample[ Go to top ]

    from Parent prnt where prnt in (SELECT chl.parent from Child chl where chl.name like 'a%')
  8. HQL Sample[ Go to top ]

    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?
  9. HQL Sample[ Go to top ]

    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 .
  10. HQL Sample[ Go to top ]

    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.