Hello,
I have implemented a GenericDao pattern.
First I tried to make a generic type GenericDao<T>, then I saw that it was even better to have a type with generic methods :-)
Here it is:
Code:
/**
* @author Alessio Pace
*
*/
public class PersistenceManagerHibernate extends HibernateDaoSupport implements PersistenceManager {
protected Logger logger = Logger.getLogger(PersistenceManager.class.getName());
public PersistenceManagerHibernate() {
super();
}
@SuppressWarnings("unchecked")
public <T> List<T> findAll(Class<T> entityClass) throws DataAccessException {
//getHibernateTemplate().setCacheQueries(true);
List<T> results = getHibernateTemplate().loadAll(entityClass);
Set<T> set = new HashSet<T>(results);
results = new ArrayList<T>(set);
return results;
}
@SuppressWarnings("unchecked")
public <T> T findById(Class<T> entityClass, Long id) throws DataAccessException {
//getHibernateTemplate().setCacheQueries(true);
T o = (T) getHibernateTemplate().get(entityClass, id);
if(o == null){
logger.warn("uh oh, document with id '" + id + "' not found...");
throw new ObjectRetrievalFailureException(entityClass, id);
}
else {
return o;
}
}
public <T> void saveOrUpdate(T entity) throws DataAccessException {
getHibernateTemplate().saveOrUpdate(entity);
}
@SuppressWarnings("unchecked")
public <T> T merge(T entity) throws DataAccessException {
return (T) getHibernateTemplate().merge(entity);
}
public <T> void remove(T entity) throws DataAccessException {
getHibernateTemplate().delete(entity);
}
public <T> void removeAll(Class<T> entityClass) throws DataAccessException {
getHibernateTemplate().deleteAll(this.findAll(entityClass));
}
@SuppressWarnings("unchecked")
public <T> List<T> findByNamedQueryAndNamedParam(Class<T> entityClass,
String queryName, String[] paramNames, Object[] values) throws DataAccessException {
List<T> results = (List<T>) getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramNames, values);
return results;
}
public <T> List<T> findByNamedQueryAndNamedParam(Class<T> entityClass,
String queryName, Map<String, ?> params) throws DataAccessException {
String[] paramNames = new String[params.size()];
Object[] values = new Object[params.size()];
List<String> keys = new ArrayList<String>(params.keySet());
for(int i=0; i<keys.size(); i++){
String k = keys.get(i);
paramNames[i] = k;
values[i] = params.get(k);
}
return this.findByNamedQueryAndNamedParam(entityClass, queryName, paramNames, values);
}
@SuppressWarnings("unchecked")
public <T> List<T> findByNamedParam(Class<T> entityClass, String query,
String[] paramNames, Object[] values) throws DataAccessException {
List<T> results = (List<T>) getHibernateTemplate().findByNamedParam(query, paramNames, values);
return results;
}
@SuppressWarnings("unchecked")
public <T> List<T> findByNamedParam(Class<T> entityClass, String query,
Map<String, ?> params) throws DataAccessException {
String[] paramNames = new String[params.size()];
Object[] values = new Object[params.size()];
List<String> keys = new ArrayList<String>(params.keySet());
for(int i=0; i<keys.size(); i++){
String k = keys.get(i);
paramNames[i] = k;
values[i] = params.get(k);
}
List<T> results = (List<T>) getHibernateTemplate().findByNamedParam(query, paramNames, values);
return results;
}
}
the class simply implements a PersistenceManager interface with the @Transactional annotation in order to be proxyed and be used by the TransactionManager :-)
I must thank a lot the author of this blog entry, because I have taken its JPA solution and "ported back" to HibernateTemplate:
http://blog.diefirma.de/2006-01-10/u...i-with-spring/
The solution IMHO works like a charm :-)
regards