com.ghy.common.orm.hibernate.HibernateDao.java Source code

Java tutorial

Introduction

Here is the source code for com.ghy.common.orm.hibernate.HibernateDao.java

Source

/**
 * Copyright (c) 2005-2010 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: HibernateDao.java 1205 2010-09-09 15:12:17Z calvinxiu $
 */
package com.ghy.common.orm.hibernate;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.ResultTransformer;
import org.springframework.util.Assert;

/**
 * ?SpringSideHibernat DAO.
 * 
 * ,?. ?Service,?DAO?,?.
 * 
 * @param <T>
 *            DAO?
 * @param <PK>
 *             
 * 
 * @author calvin
 */
@SuppressWarnings({ "unchecked", "rawtypes", "hiding" })
public class HibernateDao<T, PK extends Serializable> extends SimpleHibernateDao<T, PK> {
    /**
     * Dao?. ??Class. eg. public class UserDao extends
     * HibernateDao<User, Long>{ }
     */
    public HibernateDao() {
        super();
    }

    /**
     * ?Dao, ServiceHibernateDao. Class. eg.
     * HibernateDao<User, Long> userDao = new HibernateDao<User,
     * Long>(sessionFactory, User.class);
     */
    public HibernateDao(final SessionFactory sessionFactory, final Class<T> entityClass) {
        super(sessionFactory, entityClass);
    }

    /**
     * ?sql??map?list
     * @param sql
     * @param params
     * @return
     */
    public List<Object[]> findBySql(String sql, Map<String, ?> params) {
        SQLQuery queryObject = getSession().createSQLQuery(sql);
        if (params != null && params.size() > 0)
            queryObject.setProperties(params);
        return queryObject.list();
    }

    /**
     * ?sql???map??
     * @param sql
     * @param params
     * @return
     */
    public Integer findBySqlCount(String sql, Map<String, ?> params) {
        SQLQuery queryObject = getSession().createSQLQuery(sql);
        if (params != null && params.size() > 0)
            queryObject.setProperties(params);
        List<BigDecimal> qlist = queryObject.list();
        if (qlist != null && qlist.size() > 0) {
            BigDecimal obj = qlist.get(0);
            return obj.intValue();
        }
        return 0;
    }

    /**
     * 
     * @param sql
     * @param params
     * @return
     */
    public Float findBySqlFloat(String sql, Map<String, ?> params) {
        SQLQuery queryObject = getSession().createSQLQuery(sql);
        if (params != null && params.size() > 0)
            queryObject.setProperties(params);
        List<BigDecimal> qlist = queryObject.list();
        if (qlist != null && qlist.size() > 0) {
            BigDecimal obj = qlist.get(0);
            return obj.floatValue();
        }
        return Float.valueOf(0);
    }

    /**
     * ?sql???map?map??
     * @param sql
     * @param params
     * @return
     */
    public Integer findBySqlCount(String sql, Map<String, ?> params, Map<String, ?> params2) {
        SQLQuery queryObject = getSession().createSQLQuery(sql);
        if (params != null && params.size() > 0) {
            queryObject.setProperties(params);
        }
        if (params2 != null && params2.size() > 0) {
            queryObject.setProperties(params2);
        }
        List<BigDecimal> qlist = queryObject.list();
        if (qlist != null && qlist.size() > 0) {
            BigDecimal obj = qlist.get(0);
            return obj.intValue();
        }
        return 0;
    }

    /**
     * ? hql???maplist
     * @param sql
     * @param params
     * @return
     */
    public <T> List<T> findByHQL(String hql, Map<String, ?> params) {
        Query queryObject = getSession().createQuery(hql);
        if (params != null && params.size() > 0)
            queryObject.setProperties(params);
        return queryObject.list();
    }

    /**
     * ? hql???map
     * @param sql
     * @param params
     * @return
     */
    public void updateByHql(String hql, Map<String, ?> values) {
        Query query = getSession().createQuery(hql);
        if (values != null && values.size() > 0) {
            query.setProperties(values);
        }
        query.executeUpdate();
    }

    /**
     * ? sql???map
     * @param sql
     * @param params
     * @return
     */
    public void updateBySql(String sql, Map<String, ?> values) {
        Query query = getSession().createSQLQuery(sql);
        if (values != null && values.size() > 0) {
            query.setProperties(values);
        }
        query.executeUpdate();
    }

    /**
     * countHql.
     * 
     * ???hql?,??hql?count?.
     */
    protected long countHqlResult(final String hql, final Object... values) {
        String countHql = prepareCountHql(hql);
        System.out.println("hql count ===============" + countHql);
        try {
            Long count = findUnique(countHql, values);
            return count;
        } catch (Exception e) {
            throw new RuntimeException("hql can't be auto count, hql is:" + countHql, e);
        }
    }

    /**
     * countHql.
     * 
     * ???hql?,??hql?count?.
     */
    protected long countHqlResult(final String hql, final Map<String, ?> values) {
        String countHql = prepareCountHql(hql);

        try {
            Long count = findUnique(countHql, values);
            return count;
        } catch (Exception e) {
            throw new RuntimeException("hql can't be auto count, hql is:" + countHql, e);
        }
    }

    /**
     * countHql.
     * 
     * ???hql?,??hql?count?.
     */
    private String prepareCountHql(String orgHql) {
        String fromHql = orgHql;
        // select??order by???count,?.
        fromHql = "from " + StringUtils.substringAfter(fromHql, "from");
        fromHql = StringUtils.substringBefore(fromHql, "order by");

        String countHql = "select count(*) " + fromHql;
        return countHql;
    }

}