Hibernate Query for a List of Objects that matches a List of Objects' ids

Posted by sal on Stack Overflow See other posts from Stack Overflow or by sal
Published on 2010-05-26T16:14:57Z Indexed on 2010/05/26 16:31 UTC
Read the original article Hit count: 248

Given a classes Foo, Bar which have hibernate mappings to tables Foo, A, B and C

public class Foo {
  Integer aid;
  Integer bid;
  Integer cid;
  ...;
}

public class Bar {
  A a; 
  B b; 
  C c; 
  ...;
}

I build a List fooList of an arbitrary size and I would like to use hibernate to fetch List where the resulting list will look something like this:

Bar[1] = [X1,Y2,ZA,...]
Bar[2] = [X1,Y2,ZB,...]
Bar[3] = [X1,Y2,ZC,...]
Bar[4] = [X1,Y3,ZD,...]
Bar[5] = [X2,Y4,ZE,...]
Bar[6] = [X2,Y4,ZF,...]
Bar[7] = [X2,Y5,ZG,...]
Bar[8] = ...

Where each Xi, Yi and Zi represents a unique object.

I know I can iterate fooList and fetch each List and call barList.addAll(...) to build the result list with something like this:

List<bar> barList.addAll(s.createQuery("from Bar bar where bar.aid = :aid and ... ")
    .setEntity("aid", foo.getAid())
    .setEntity("bid", foo.getBid())
    .setEntity("cid", foo.getCid())
    .list();
    );

Is there any easier way, ideally one that makes better use of hibernate and make a minimal number of database calls?

Am I missing something? Is hibernate not the right tool for this?

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate