Hello,
I'm using Christian Bauer's example of a GenericDAO as a base class for my own DAOs. If you're not familiar with it, here's the relevant code:
Code:
public abstract class GenericDAO<T,ID extends Serializable> {
public List<T> findByCriteria(Criterion... criterion) {
Session session =
((HibernateEntityManager)getEntityManager()).getSession();
Criteria crit = session.createCriteria(getEntityBeanType());
for (Criterion c : criterion) {
crit.add(c);
}
return crit.list();
}
....
}
I'm looking to add a similar method that returns results as a Map<ID,T> instead of a List<T>. At first I thought projections would be the answer, something like the following:
Code:
public Map<ID,T> findByCriteriaMap(Criterion... criterion) {
Session session =
((HibernateEntityManager)getEntityManager()).getSession();
Criteria crit = session.createCriteria(getEntityBeanType());
for (Criterion c : criterion) {
crit.add(c);
}
crit.setProjection( Projections.projectionList()
.add( Projections.id() )
.add( Projections.????? )
);
List<?> list = crit.list();
Map<ID,T> map = new HashMap<ID,T>();
for( Object result : list ) {
Object[] arr = (Object[]) result;
ID id = (ID) arr[0];
T entity = (T) arr[1];
map.put( id, entity );
}
return map;
}
However, this solution requires a projection that returns the entity itself, and not aggregate information or a specific property on that entity. I can't find a ResultTransformer to do this for me nor have I entered a Google query that has provided illumination.
Any suggestions?
Thanks,
Joel