Created
February 26, 2014 09:02
-
-
Save allanruin/9226155 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.irix.dao; | |
import java.lang.reflect.*; | |
import java.util.List; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.apache.logging.log4j.Marker; | |
import org.apache.logging.log4j.MarkerManager; | |
import org.hibernate.LockOptions; | |
import org.hibernate.Query; | |
import org.hibernate.Session; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.criterion.Example; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Repository; | |
@Repository | |
public class BaseDAO<T> { | |
private Class<T> clazz; | |
private SessionFactory sessionFactory; | |
private Logger logger = LogManager.getLogger(BaseDAO.class); | |
private static final Marker DAO_MARKER = MarkerManager.getMarker("DAO"); | |
// public Class<T> getClazz() { | |
// return clazz; | |
// } | |
// | |
// @Autowired | |
// public void setClazz(Class<T> clazz) { | |
// this.clazz = clazz; | |
// } | |
@SuppressWarnings("unchecked") | |
public BaseDAO() { | |
super(); | |
clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; | |
} | |
// @Autowired | |
// public BaseDAO(Class<T> entityClass){ | |
// this.clazz = entityClass; | |
// } | |
public SessionFactory getSessionFactory() { | |
return sessionFactory; | |
} | |
@Autowired | |
public void setSessionFactory(SessionFactory sessionFactory) { | |
this.sessionFactory = sessionFactory; | |
} | |
public Session getSession() { | |
return sessionFactory.getCurrentSession(); | |
} | |
public void save(T instance){ | |
// logger.debug(DAO_MARKER,"Saving instance"); | |
getSession().save(instance); | |
} | |
public void delete(T instance){ | |
getSession().delete(instance); | |
} | |
@SuppressWarnings("unchecked") | |
public T findById(java.lang.Integer id) { | |
logger.debug(DAO_MARKER,"getting " + clazz + " instance with id: " + id); | |
try { | |
T instance = (T) getSession().get(clazz,id); | |
return instance; | |
} catch (RuntimeException re) { | |
logger.error("get " + clazz + " failed", re); | |
throw re; | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public List<T> findByExample(T instance) { | |
logger.debug(DAO_MARKER,"finding " + clazz + " instance by example"); | |
try { | |
List<T> results = getSession() | |
.createCriteria(clazz) | |
.add(Example.create(instance)).list(); | |
logger.debug(DAO_MARKER,"find by example successful, result size: " | |
+ results.size()); | |
return results; | |
} catch (RuntimeException re) { | |
logger.error("find " + clazz + " by example failed", re); | |
throw re; | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public List<T> findByProperty(String propertyName, Object value) { | |
logger.debug(DAO_MARKER,"finding " + clazz + "instance with property: " | |
+ propertyName + ", value: " + value); | |
try { | |
String queryString = "from " + clazz + " as model where model." | |
+ propertyName + "= ?"; | |
Query queryObject = getSession().createQuery(queryString); | |
queryObject.setParameter(0, value); | |
return queryObject.list(); | |
} catch (RuntimeException re) { | |
logger.error("find " + clazz + " by property name failed", re); | |
throw re; | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public List<T> findAll() { | |
logger.debug(DAO_MARKER,"finding all " + clazz + " instances"); | |
try { | |
String queryString = "from "+ clazz; | |
Query queryObject = getSession().createQuery(queryString); | |
return (List<T>)queryObject.list(); | |
} catch (RuntimeException re) { | |
logger.error("find all failed", re); | |
throw re; | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public List<T> findAllBeginAt(int left,int len) { | |
logger.debug(DAO_MARKER,"finding " + clazz + " between " + left | |
+ " and " + len); | |
try { | |
String queryString = "from "+ clazz; | |
Query queryObject = getSession().createQuery(queryString); | |
queryObject.setFirstResult(left); | |
queryObject.setMaxResults(len); | |
return (List<T>)queryObject.list(); | |
} catch (RuntimeException re) { | |
logger.error("find all failed", re); | |
throw re; | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public T merge(T detachedInstance) { | |
logger.debug(DAO_MARKER,"merging " + clazz + " instance"); | |
try { | |
T result = (T) getSession().merge(detachedInstance); | |
logger.debug(DAO_MARKER,"merge successful"); | |
return result; | |
} catch (RuntimeException re) { | |
logger.error("merge failed", re); | |
throw re; | |
} | |
} | |
public void attachDirty(T instance) { | |
logger.debug(DAO_MARKER,"attaching dirty " + clazz + " instance"); | |
try { | |
getSession().saveOrUpdate(instance); | |
logger.debug(DAO_MARKER,"attach successful"); | |
} catch (RuntimeException re) { | |
logger.error("attach failed", re); | |
throw re; | |
} | |
} | |
public void attachClean(T instance) { | |
logger.debug(DAO_MARKER,"attaching clean " + clazz + " instance"); | |
try { | |
getSession().buildLockRequest(LockOptions.NONE).lock(instance); | |
logger.debug(DAO_MARKER,"attach successful"); | |
} catch (RuntimeException re) { | |
logger.error("attach failed", re); | |
throw re; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment