This is my situation, I have two basic POJO\'s which I\'ve given a simple hibernate mapping :
Person
- PersonId
- Name
- Books
Book
- Code
- Descr
Expanding on Mathews answer. To force hibernate to only return a list of persons do:
List peopleWithBooks = session.createSQLQuery(
"select {p.*}, {b.*} from person p, book b where ").
.addEntity("p", Person.class)
.addJoin("b", "p.books")
.addEntity("p", Person.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
Associated Book entities will be fetched and initialized without a additional call to the db.
The duplicate
.addEntity("p", Person.class)
is necessary because
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
operates on the last entity added.