Does anyone have a nice way to take advantage of fetching the parent + child data in one database visit while minimising clutter in the DAO layer?
e.g.
session.createQuery("select p from Person p left join fetch p.addresses where p.id = ?", id).uniqueResult();
will retrieve a single Person and all of their Addresses in a single trip to the DB. So will the following:
session.createCriteria(Person.class)
.add(Restrictions.idEq(someId)
.setFetchMode("addresses", FetchMode.JOIN);
What's the best approach out of:
1. Adding additional DAO methods as required which pre-fetch differing amounts of data using HQL, or
2. Parameterising DAO methods with the additional child data they should retrieve, and possibly using the session.createCriteria approach instead of HQL, or
3. Other?
We've currently got a business logic layer and a DAO layer, but seems like adding some additional service/use-case layer might be a good place to handle these considerations. The thing is, you probably still don't want HQL leaking up into any service layer.
Thanks,


Reply With Quote