From a6a687c8e47d3527e255ed91de8a3694eb81748d Mon Sep 17 00:00:00 2001 From: vovasty Date: Wed, 18 Feb 2009 16:53:22 +0300 Subject: [PATCH] commn dao --- .../commons/hibernate/dao/GenericDAO.java | 17 ++++ .../commons/hibernate/dao/GenericHibernateDAO.java | 92 ++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 aramzamzam-commons/commons-hibernate/src/main/java/net/aramzamzam/commons/hibernate/dao/GenericDAO.java create mode 100644 aramzamzam-commons/commons-hibernate/src/main/java/net/aramzamzam/commons/hibernate/dao/GenericHibernateDAO.java diff --git a/aramzamzam-commons/commons-hibernate/src/main/java/net/aramzamzam/commons/hibernate/dao/GenericDAO.java b/aramzamzam-commons/commons-hibernate/src/main/java/net/aramzamzam/commons/hibernate/dao/GenericDAO.java new file mode 100644 index 0000000..ef7fadb --- /dev/null +++ b/aramzamzam-commons/commons-hibernate/src/main/java/net/aramzamzam/commons/hibernate/dao/GenericDAO.java @@ -0,0 +1,17 @@ +package ru.rentdom.dao; + +import java.io.Serializable; +import java.util.List; +/** DAO pattern from "Generic Data Access Objects": http://www.hibernate.org/328.html*/ +public interface GenericDAO { + + T findById(ID id); + + List findAll(); + + List findByExample(T exampleInstance); + + T makePersistent(T entity); + + void makeTransient(T entity); +} diff --git a/aramzamzam-commons/commons-hibernate/src/main/java/net/aramzamzam/commons/hibernate/dao/GenericHibernateDAO.java b/aramzamzam-commons/commons-hibernate/src/main/java/net/aramzamzam/commons/hibernate/dao/GenericHibernateDAO.java new file mode 100644 index 0000000..dc20b00 --- /dev/null +++ b/aramzamzam-commons/commons-hibernate/src/main/java/net/aramzamzam/commons/hibernate/dao/GenericHibernateDAO.java @@ -0,0 +1,92 @@ +package ru.rentdom.dao; + +import java.io.Serializable; +import java.lang.reflect.ParameterizedType; +import java.util.List; + +import org.apache.tapestry5.hibernate.HibernateSessionManager; +import org.hibernate.Criteria; +import org.hibernate.Session; +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Example; + +/** DAO pattern from "Generic Data Access Objects": http://www.hibernate.org/328.html*/ +public class GenericHibernateDAO + implements GenericDAO { + + private Class persistentClass; + private final HibernateSessionManager sessionManager; + + @SuppressWarnings("unchecked") + public GenericHibernateDAO(HibernateSessionManager sessionManager){ + this.persistentClass = (Class) ((ParameterizedType) getClass() + .getGenericSuperclass()).getActualTypeArguments()[0]; + this.sessionManager = sessionManager; + } + + protected Session getSession() { + return sessionManager.getSession(); + } + + public Class getPersistentClass() { + return persistentClass; + } + + public List findAll() { + return findByCriteria(); + } + + @SuppressWarnings("unchecked") + public List findByExample(X exampleInstance, String... excludeProperty) { + Criteria crit = getSession().createCriteria(exampleInstance.getClass()); + Example example = Example.create(exampleInstance); + for (String exclude : excludeProperty) { + example.excludeProperty(exclude); + } + crit.add(example); + return crit.list(); + } + + public List findByExample(T exampleInstance) { + return findByExample(exampleInstance,new String[0]); + } + + public T makePersistent(T entity) { + getSession().saveOrUpdate(entity); + return entity; + } + + public void makeTransient(T entity) { + getSession().delete(entity); + } + + public void flush() { + getSession().flush(); + } + + public void clear() { + getSession().clear(); + } + + /** + * Use this inside subclasses as a convenience method. + */ + @SuppressWarnings("unchecked") + protected List findByCriteria(Criterion... criterion) { + Criteria crit = getSession().createCriteria(getPersistentClass()); + for (Criterion c : criterion) { + crit.add(c); + } + return crit.list(); + } + + /** + * Возвращает null, если entity не была найдена + * @param id + * @return + */ + @SuppressWarnings("unchecked") + public T findById(ID id) { + return (T) getSession().get(getPersistentClass(), id); + } +} -- 2.11.4.GIT