team.curise.dao.BaseDao.java Source code

Java tutorial

Introduction

Here is the source code for team.curise.dao.BaseDao.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package team.curise.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.util.Assert;
import team.curise.page.entity.Page;

/**
 *
 * @author pupu
 */
public class BaseDao<T> {
    private Class<T> entityClass;
    @Autowired
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public Session getSession() {
        return this.getHibernateTemplate().getSessionFactory().openSession();
    }

    /**
     * ????
     */
    public BaseDao() {
        Type rootType = getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) rootType).getActualTypeArguments();
        entityClass = (Class) params[0];
    }

    /**
     * identity
     * @param id
     * @return
     */
    public T get(Serializable id) {
        return (T) getHibernateTemplate().get(entityClass, id);
    }

    /**
     * ?entity
     * @param entity
     */
    public void save(T entity) {
        Session session = getSession();
        Transaction tx = null;
        tx = session.beginTransaction();
        session.save(entity);
        tx.commit();
        session.flush();
    }

    /**
     * entity
     * @param entity
     */
    public void delete(T entity) {
        Session session = getSession();
        Transaction tx = null;
        tx = session.beginTransaction();
        session.delete(entity);
        tx.commit();
        session.flush();
    }

    /**
     * entity
     * @param entity
     */
    public void update(T entity) {
        Session session = getSession();
        Transaction tx = null;
        tx = session.beginTransaction();
        session.update(entity);
        tx.commit();
        session.flush();
    }

    /**
     * ?
     * @param entity
     */
    public void initialize(T entity) {
        this.getHibernateTemplate().initialize(entity);
    }

    /**
     * ??hql
     * @param hql
     * @return
     */
    public List find(String hql) {
        return this.getHibernateTemplate().find(hql);
    }

    /**
     * ?hql
     * @param hql
     * @return
     */
    public List find(String hql, Object... params) {
        return this.getHibernateTemplate().find(hql, params);
    }

    /**
     * hqlselect ??union,pagedQuery.
     * @see #pagedQuery(String,int,int,Object[])
     * @param hql
     * @return
     */
    public static String removeSelect(String hql) {
        Assert.hasText(hql);
        int beginPos = hql.toLowerCase().indexOf("from");
        Assert.isTrue(beginPos != -1, " hql : " + hql + " must has a keyword 'from'");
        return hql.substring(beginPos);
    }

    /**
     * orderby??
     * @param hql
     * @return
     */
    public static String removeOrders(String hql) {
        Assert.hasText(hql);
        Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(hql);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, "");
        }
        m.appendTail(sb);
        return sb.toString();
    }

    /**
     * Query. ?first,max,fetchsize,cache,cacheRegion,?Query?.
     * ??,
     * <pre>
     * dao.getQuery(hql).setMaxResult(100).setCacheable(true).list();
     * </pre>
     * ?
     * <pre>
     *        dao.createQuery(hql)
     *        dao.createQuery(hql,arg0);
     *        dao.createQuery(hql,arg0,arg1);
     *        dao.createQuery(hql,new Object[arg0,arg1,arg2])
     * </pre>
     *
     * @param params ???.
     */
    public Query createQuery(String hql, Object... params) {
        Assert.hasText(hql);
        Query query = getSession().createQuery(hql);
        for (int i = 0; i < params.length; i++) {
            query.setParameter(hql, params[i]);
        }
        return query;
    }

    /**
     * 
     * @param hql
     * @param params
     * @return
     */
    public long findTotalCount(String hql, Object... params) {
        List countList;
        String countQueryString = " select count (*) " + removeSelect(removeOrders(hql));
        countList = find(countQueryString, params);
        return (Long) countList.get(0);
    }

    /**
     * 
     * @param hql
     * @param pageNo
     * @param pageSize
     * @param params
     * @return
     */
    public Page pageQuery(String hql, int pageNo, int pageSize, Object... params) {
        Assert.hasText(hql);
        Assert.isTrue(pageNo >= 1, "page number should start from 1");
        long totalCount = findTotalCount(hql, params);
        if (totalCount < 1) {
            return new Page();
        }
        int startIndex = Page.getStartOfPage(pageNo, pageSize);
        Query query = createQuery(hql, params);
        //startIndex?pageSize?
        List list = query.setFirstResult(startIndex).setMaxResults(pageSize).list();
        return new Page(pageSize, startIndex, list, totalCount);
    }

    public List listQuery(String hql, Object... params) {
        Query query = createQuery(hql, params);
        List list = query.list();
        return list;
    }
}