GenericDao class
程序员文章站
2022-07-11 23:48:57
...
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; } }
上一篇: memcached集群-magent
下一篇: Oracle 多行转列方法