-
-
-
-
-
-
-
- public abstract class GenericDAOHibernate 〈T,ID extends Serializable, DAOImpl extends IGenericDAO〈T,ID〉〉
- implements IGenericDAO〈T,ID〉
- {
- private Class persistentClass;
-
- protected Session session;
-
- public GenericDAOHibernate()
- {
- this.persistentClass = (Class) ((ParameterizedType) getClass()
- .getGenericSuperclass()).getActualTypeArguments()[0];
- }
-
- @SuppressWarnings("unchecked")
- public DAOImpl setSession(Session s)
- {
- this.session = s;
- return (DAOImpl)this;
- }
-
- protected Session getSession()
- {
- if (session == null)
- throw new IllegalStateException(
- "Session has not been set on DAO before usage");
- return session;
- }
-
- public Class getPersistentClass()
- {
- return persistentClass;
- }
-
-
- @SuppressWarnings("unchecked")
- public T findById(ID id)
- {
- return (T) getSession().load(getPersistentClass(), id);
- }
-
- @SuppressWarnings("unchecked")
- public T findById(ID id, boolean lock)
- {
- T entity;
- if (lock)
- entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
- else
- entity = findById(id);
-
- return entity;
- }
-
- @SuppressWarnings("unchecked")
- public List findAll()
- {
- return findByCriteria();
- }
-
- @SuppressWarnings("unchecked")
- public List findByExample(T exampleInstance)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- Example example = Example.create(exampleInstance);
- crit.add(example);
- return crit.list();
- }
-
- @SuppressWarnings("unchecked")
- public List findByExample(T exampleInstance, String[] excludeProperty)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- Example example = Example.create(exampleInstance);
- for (String exclude : excludeProperty)
- {
- example.excludeProperty(exclude);
- }
- crit.add(example);
- return crit.list();
- }
-
- @SuppressWarnings("unchecked")
- public T makePersistent(T entity)
- {
- getSession().saveOrUpdate(entity);
-
- return entity;
- }
-
- public void makeTransient(T entity)
- {
- getSession().delete(entity);
- }
-
- @SuppressWarnings("unchecked")
- protected List findByCriteria(Criterion... criterion)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- for (Criterion c : criterion)
- {
- crit.add(c);
- }
- return crit.list();
- }
-
- @SuppressWarnings("unchecked")
-
-
-
- protected List findByCriteria(Order order,Criterion... criterion)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- for (Criterion c : criterion)
- {
- crit.add(c);
- }
- if(order!=null)
- crit.addOrder(order);
- return crit.list();
- }
-
- @SuppressWarnings("unchecked")
- protected List findByCriteria(int firstResult,int rowCount,Order order,Criterion... criterion)
- {
- Criteria crit = getSession().createCriteria(getPersistentClass());
- for (Criterion c : criterion)
- {
- crit.add(c);
- }
- if(order!=null)
- crit.addOrder(order);
- crit.setFirstResult(firstResult);
- crit.setMaxResults(rowCount);
- return crit.list();
- }
- }
--------------------next---------------------
阅读(354) | 评论(0) | 转发(0) |