views:

826

answers:

2

One thing I like about EclipseLink has this great thing called the batch query hint, which I'm yet to find a Hibernate equivalent of.

Basically doing a whole bunch of joins gets messy real quick and you end up querying way more data than you necessarily want (remember that if you join person to 6 addresses the person information is returned 6 times; now keep multiplying that out by extra joins).

Imagine a Person entity with 0:M collections of Address, Email, Phone and OrderHistory. Joining all that is not good but with the batch method:

List persons = entityManager.createQuery("select p from Person p"
  .setHint(QueryHints.BATCH, "p.address")
  .setHint(QueryHints.BATCH, "p.email")
  .setHint(QueryHints.BATCH, "p.phone")
  .setHint(QueryHints.BATCH, "p.orderHistory")
  .getResultList();

This will do a query on the Person table and that's it. When you first access an address record it will do a single query for the entire Address table. If you specified a where clause on the Person table, this same criteria will be used for the Address load too.

So instead of doing 1 query, you do 5.

If you were doing that with joins you might get it all in one query but you may very well be loading way more data because of the joins.

Anyway, I've gone looking in the Hibernate docs for an equivalent to this but don't see one. Is there one?

A: 

There are two things I know of that might help:

1) hibernate.default_batch_fetch_size

2) Criteria.setFetchMode and Criteria.setFetchSize

Stephen Denne
+1  A: 

There isn't one.

cletus