일반적인 DAO 쿼리, 일반적인 방법 집합

9063 단어
import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class GenericDao extends HibernateDaoSupport{
    private ThreadLocal sessions = new ThreadLocal();
    private ThreadLocal transactions = new ThreadLocal();
    public synchronized Session getMySession(){
        Session session = sessions.get();
        if(session==null){
            session = getSession();
            transactions.set(session.beginTransaction());
            sessions.set(session);
        }else
        if(!session.isOpen()){
            session = getSession();
            transactions.set(session.beginTransaction());
            sessions.set(session);
        }
        return session;
    }
    public synchronized void commitMySession(){
        Session session = sessions.get();
        if(session!=null&&session.isOpen()){
            transactions.get().commit();
            session.close();
        }
        transactions.remove();
        sessions.remove();
    }
    public synchronized void rollbackMySession(){
        Session session = sessions.get();
        if(session!=null&&session.isOpen()){
            transactions.get().rollback();
            session.close();
        }
        transactions.remove();
        sessions.remove();
    }
    public  T get(Class clazz, Serializable id){
        return (T)getHibernateTemplate().get(clazz, id);
    }
    public  T load(Class clazz, Serializable id){
        return (T)getHibernateTemplate().load(clazz, id);
    }
    public  void save(T entity){
//        System.out.println("---->gdao.save("+entity.getClass().getName()+")----");
        getHibernateTemplate().save(entity);
    }
    public  void update(T entity){
        getHibernateTemplate().update(entity);
    }
    public  void delete(T entity){
        getHibernateTemplate().delete(entity);
    }
   
    public int execUpdateSQL(String sql, Serializable...values){
        Session sess = getMySession();
        SQLQuery query = sess.createSQLQuery(sql);
        if(values!=null&&values.length>0){
            for(int i=0; i params = new ArrayList(1);
        List types = new ArrayList(1);
        params.add("c"); types.add(Hibernate.INTEGER);
        Object obj = findUnique(sql, params, types, values);
        if(obj==null){
            return -1L;
        }
        if(obj instanceof Object[]){
            obj = ((Object[])obj)[0];
        }
        if(obj instanceof Number){
            return ((Number)obj).longValue();
        }
        return -1L;
    }
//    public Long getFirstLong(String sql,  Listparams, Listtypes) throws Exception{
//        Object[] obj = findUnique(sql, params, types);
//        if(obj==null||obj.length==0){
//            return -1L;
//        }
//        Object object = obj[0];
//        if(object instanceof Number){
//            return ((Number)object).longValue();
//        }
//        return -1L;
//    }
    public List> find(String hql){
        return getHibernateTemplate().find(hql);
    }
//    public List> find(String hql, final Object...values){
//        logger.info(hql);
//        return getHibernateTemplate().find(hql, values);
//    }
   
    public  List findByPage(Class clazz, final int start, final int limit, final String where, final Serializable... values){
        String hql = "from " + clazz.getName() + " as e";
        if(where!=null&&where.length()>0){
            hql += " where " + where;
        }
        final String fHql = hql;
        return getHibernateTemplate().executeFind(new HibernateCallback(){

            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Query q = session.createQuery(fHql);
                if(values!=null&&values.length>0){
                    for(int i=0; i List find(Class clazz, final String where, final Serializable... values){
        String hql = "from " + clazz.getSimpleName()+" as e";
        if(where!=null&&where.length()>0){
            hql += " where " + where;
        }
        return getHibernateTemplate().find(hql, values);
    }
    public long getTotalCountByHql(Class> clazz, final String where, final Serializable... values) throws Exception {
        String hql = "select count(*) from " + clazz.getSimpleName()+" as e";
        if(where!=null&&where.length()>0){
            hql += " where " + where;
        }
        List cs = getHibernateTemplate().find(hql, values);
        if(cs!=null&&cs.size()>0){
            Number n = cs.get(0);
            return n.longValue();
        }
        return 0;
    }
    public long getTotalCount(Class> clazz, final String where, final Serializable... values) throws Exception {
        String sql = "select count(1) as c from " + clazz.getSimpleName() + " e";
        if(where!=null&&where.length()>0){
            sql += " where " + where;
        }
        return getFirstLong(sql, values);
    }
   
    public long getTotalCount(String sql, final Serializable... values) throws Exception {
        return getFirstLong(sql, values).longValue();
    }
    public Object[] findUnique(final String sql,  Listparams, Listtypes, final Serializable...values) {
        Session sess = getMySession();
        logger.debug("------findUnique.getSession()! sess="+sess.hashCode());
        SQLQuery query = sess.createSQLQuery(sql);
        for(int j=0; j0){
            for(int i=0; i list = query.list();
        if(list==null||list.size()==0){
            return null;
        }
        Object obj = list.get(0);
        if(obj==null){
            return null;
        }
        logger.debug("obj.type="+obj.getClass().getName());
        if(!obj.getClass().isArray()){//         ,         
            if(obj instanceof Number){
                if(obj instanceof Long){
                    return new Long[]{(Long)(obj)};
                }
                if(obj instanceof Integer){
                    return new Long[]{new Long((Integer)obj)};
                }
                return new Number[]{(Number)obj};
            }
            return new Object[]{obj};
        }
        return (Object[])obj;
    }
    public List find(final String sql, Listparams, Listtypes, final Serializable... values) {
        Session sess = getMySession();
        logger.debug("------find.getSession()! sess="+sess.hashCode());
        SQLQuery query = sess.createSQLQuery(sql);
       
        for(int j=0; j0){
            for(int i=0; i list = query.list();
        if(list==null||list.size()==0){
            return -1L;
        }
        Object obj = list.get(0);
        logger.debug("obj.type="+obj.getClass().getName());
        if(obj instanceof Number){
            return ((Number)obj).longValue();
        }
        return -1L;
    }
    public boolean exists(String sql, final Object...values) throws Exception{
        Session sess = getMySession();
        logger.debug("------exists.getSession()! sess="+sess.hashCode());
        SQLQuery query = sess.createSQLQuery(sql);
        for(int i=0; i list = query.list();
        if(list==null||list.size()==0){
            return false;
        }
        return true;
    }

}

좋은 웹페이지 즐겨찾기