Currently I have one dao, that uses generic methods and hibernate criteria to perform nearly all my data access (I planned to subclass for the more complex things later), it looks like this :
public <T> List<T> getAll(final Class<T> type){
final Session session = sessionFactory.getCurrentSession();
final Criteria crit = session.createCriteria(type);
return crit.list();
}
public <T> List<T> getFieldEq(final Class<T> type, final String propertyName, final Object value){
final Session session = sessionFactory.getCurrentSession();
final Criteria crit = session.createCriteria(type);
crit.add(Restrictions.eq(propertyName, value));
return crit.list();
}
However, HQL is preferable as it can optimized by the database/connection (i.e. parametrized queries), while the criteria api has to be evaluated at runtime, so Restrictions.eq("name", "NimChimpksy")
is not error safe at all.
Should I keep the generic dao (it feels nice to have only one dao), or just implement a common interface and use hql in a separate dao for each of my domain objects.