views:

755

answers:

4

is possible with Hibernate criteria do it?

select A.something, B.something, C.something, D.something
    from  A JOIN B on A.id = B.id_fk
          JOIN C ON B.id = C.id_fk
          JOIN D ON C.id = D.id_fk;
A: 

Try setting the fetch mode in your criteria, like:

criteria.setFetchMode(..., FetchMode.EAGER)

This creates a join query. You may find more details here.

Péter Török
A: 

There are some good examples in the Hibernate Reference material that show to use setFetchMode to fetch associations with an outer join.

An example is:

List books = sess.createCriteria(Book.class)
.setFetchMode("chapters", FetchMode.EAGER)
.setFetchMode("reviews", FetchMode.EAGER)
.list();

There is also information there about different fetching stragies that may be of use to you.

Rachel
A: 
boxymoron
The question asked for a solution with the criteria API, not with SQLQuery. Also, your solution is vastly more complicated than necessary.
meriton
I digress, just trying to show an alternative...
boxymoron
A: 

Yes, in fact there are several ways of doing this:

  1. When mapping the association, set its lazyness to false and its fetch mode to join. This will affect all criteria queries.
  2. Use setFetchMode as detailed by the other answers.
  3. Use criteria.createAlias (or createCriteria). This also allows you to further restrict the rows you want joined.
meriton