org.jessma.dao.hbn.HibernateFacade.java Source code

Java tutorial

Introduction

Here is the source code for org.jessma.dao.hbn.HibernateFacade.java

Source

/*
 * Copyright Bruce Liang (ldcsaa@gmail.com)
 *
 * Version   : JessMA 3.5.1
 * Author   : Bruce Liang
 * Website   : http://www.jessma.org
 * Project   : http://www.oschina.net/p/portal-basic
 * Blog      : http://www.cnblogs.com/ldcsaa
 * WeiBo   : http://weibo.com/u/1402935851
 * QQ Group   : 75375912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jessma.dao.hbn;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.type.Type;
import org.jessma.dao.AbstractFacade;
import org.jessma.util.KV;

/**
 * 
 * @author Kingfisher
 * 
 * ?Session?? / ?
 *    1?HQL  / 
 *    2?QBC  / 
 *    3?SQL  / 
 *
 * ?? DAOException
 * 
 * 
 *    1?DAO? Hibernate
 *    2?? Facade ? Hibernate
 * 
 */
@SuppressWarnings("unchecked")
public abstract class HibernateFacade extends AbstractFacade<HibernateSessionMgr, Session> {
    protected HibernateFacade(HibernateSessionMgr mgr) {
        super(mgr);
    }

    /* ************************************************************************************************** */
    /* **********************************************  ********************************************* */

    /**
     * 
     * ??
     * 
     * @param o   : ??
     * @return   : 
     * 
     */
    protected <T> Serializable save(T o) {
        return getSession().save(o);
    }

    /**
     * 
     * ??
     * 
     * @param objs   : ??
     * @return      : 
     * 
     */
    protected <T> List<Serializable> save(List<T> objs) {
        List<Serializable> ids = new ArrayList<Serializable>(objs.size());

        for (int i = 0; i < objs.size(); i++)
            ids.set(i, save(objs.get(i)));

        return ids;
    }

    /**
     * 
     * ?
     * 
     * @param o   : ?
     * 
     */
    protected <T> void update(T o) {
        getSession().update(o);
    }

    /**
     * 
     * ?
     * 
     * @param objs   : ?
     * 
     */
    protected <T> void update(List<T> objs) {
        for (Object o : objs)
            update(o);
    }

    /**
     * 
     * ??
     * 
     * @param o   : ??
     * 
     */
    protected <T> void saveOrUpdate(T o) {
        getSession().saveOrUpdate(o);
    }

    /**
     * 
     * ??
     * 
     * @param objs   : ??
     * 
     */
    protected <T> void saveOrUpdate(List<T> objs) {
        for (Object o : objs)
            saveOrUpdate(o);
    }

    /**
     * 
     * ?
     * 
     * @param o   : ?
     * 
     */
    protected <T> void delete(T o) {
        getSession().delete(o);
    }

    /**
     * 
     * ?
     * 
     * @param objs   : ?
     * 
     */
    protected <T> void delete(List<T> objs) {
        for (Object o : objs)
            delete(o);
    }

    /**
     * 
     * ??
     * 
     * @param o   : ??
     * @return   : ??
     * 
     */
    protected <T> T merge(T o) {
        return (T) getSession().merge(o);
    }

    /**
     * 
     * ??
     * 
     * @param objs   : ??
     * @return      : ??
     * 
     */
    protected <T> List<T> merge(List<T> objs) {
        List<T> os = new ArrayList<T>(objs.size());

        for (int i = 0; i < objs.size(); i++)
            os.set(i, merge(objs.get(i)));

        return os;
    }

    /**
     * 
     * ??
     * 
     * @param objClass   : ?
     * @param id      : 
     * @return         : ? NULL
     * 
     */
    protected <T> T get(Class<T> objClass, Serializable id) {
        return (T) getSession().get(objClass, id);
    }

    /**
     * 
     * ??
     * 
     * @param objClass   : ?
     * @param ids      : 
     * @return         : ? NULL
     * 
     */
    protected <T> List<T> get(Class<T> objClass, List<Serializable> ids) {
        List<T> os = new ArrayList<T>(ids.size());

        for (int i = 0; i < ids.size(); i++)
            os.set(i, get(objClass, ids.get(i)));

        return os;
    }

    /**
     * 
     * ??
     * 
     * @param objClass   : ?
     * @param id      : 
     * @return         : ?
     * 
     */
    protected <T> T load(Class<T> objClass, Serializable id) {
        return (T) getSession().load(objClass, id);
    }

    /**
     * 
     * ??
     * 
     * @param objClass   : ?
     * @param ids      : 
     * @return         : ?
     * 
     */
    protected <T> List<T> load(Class<T> objClass, List<Serializable> ids) {
        List<T> os = new ArrayList<T>(ids.size());

        for (int i = 0; i < ids.size(); i++)
            os.set(i, load(objClass, ids.get(i)));

        return os;
    }

    /**
     * 
     * 
     * 
     * @param o   : ?
     * 
     */
    protected <T> void evict(T o) {
        getSession().evict(o);
    }

    /**
     * 
     * 
     * 
     * @param objs   : ?
     * 
     */
    protected <T> void evict(List<T> objs) {
        for (Object o : objs)
            evict(o);
    }

    /**  {@link Session}  */
    protected void clear() {
        getSession().clear();
    }

    /**  {@link Session}  */
    protected void flush() {
        getSession().flush();
    }

    /**
     * 
     * HQL
     * 
     * @param hql      : HQL ?
     * @param params   : ?
     * @return      : 
     * 
     */
    protected <T> List<T> hqlQuery(String hql, Object... params) {
        return hqlQuery(0, 0, hql, params);
    }

    /**
     * 
     * ?HQL
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param hql         : HQL ?
     * @param params      : ?
     * @return            : 
     * 
     */
    protected <T> List<T> hqlQuery(int firstResult, int maxResults, String hql, Object... params) {
        Query query = getSession().createQuery(hql);

        for (int i = 0; i < params.length; i++)
            query.setParameter(i, params[i]);

        if (firstResult > 0)
            query.setFirstResult(firstResult);
        if (maxResults > 0)
            query.setMaxResults(maxResults);

        return query.list();
    }

    /**
     * 
     * ????HQL
     * 
     * @param hql      : HQL ?
     * @param params   : ?????????
     * @return         : 
     * 
     */
    protected <T> List<T> hqlQuery2(String hql, KV<String, Object>... params) {
        return hqlQuery2(0, 0, hql, params);
    }

    /**
     * 
     * ?????HQL
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param hql         : HQL ?
     * @param params      : ?????????
     * @return            : 
     * 
     */
    protected <T> List<T> hqlQuery2(int firstResult, int maxResults, String hql, KV<String, Object>... params) {
        Query query = getSession().createQuery(hql);

        for (KV<String, Object> kv : params)
            query.setParameter(kv.getKey(), kv.getValue());

        if (firstResult > 0)
            query.setFirstResult(firstResult);
        if (maxResults > 0)
            query.setMaxResults(maxResults);

        return query.list();
    }

    /**
     * 
     * HQL
     * 
     * @param hql         : HQL?
     * @param params      : ?
     * @return            : ?
     * 
     */
    protected int hqlUpdate(String hql, Object... params) {
        Query query = getSession().createQuery(hql);

        for (int i = 0; i < params.length; i++)
            query.setParameter(i, params[i]);

        return query.executeUpdate();
    }

    /**
     * 
     * ????HQL
     * 
     * @param hql         : HQL?
     * @param params      : ?????????
     * @return            : ?
     * 
     */
    protected int hqlUpdate2(String hql, KV<String, Object>... params) {
        Query query = getSession().createQuery(hql);

        for (KV<String, Object> kv : params)
            query.setParameter(kv.getKey(), kv.getValue());

        return query.executeUpdate();
    }

    /**
     * 
     * ??
     * 
     * @param queryName      : ??
     * @param params      : ?
     * @return            : 
     * 
     */
    protected <T> List<T> namedQuery(String queryName, Object... params) {
        return namedQuery(0, 0, queryName, params);
    }

    /**
     * 
     * ???
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param queryName      : ??
     * @param params      : ?
     * @return            : 
     * 
     */
    protected <T> List<T> namedQuery(int firstResult, int maxResults, String queryName, Object... params) {
        Query query = getSession().getNamedQuery(queryName);

        for (int i = 0; i < params.length; i++)
            query.setParameter(i, params[i]);
        if (firstResult > 0)
            query.setFirstResult(firstResult);
        if (maxResults > 0)
            query.setMaxResults(maxResults);

        return query.list();
    }

    /**
     * 
     * ??????
     * 
     * @param queryName      : ??
     * @param params      : ?????????
     * @return            : 
     * 
     */
    protected <T> List<T> namedQuery2(String queryName, KV<String, Object>... params) {
        return namedQuery2(0, 0, queryName, params);
    }

    /**
     * 
     * ??????
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param queryName      : ??
     * @param params      : ?????????
     * @return            : 
     * 
     */
    protected <T> List<T> namedQuery2(int firstResult, int maxResults, String queryName,
            KV<String, Object>... params) {
        Query query = getSession().getNamedQuery(queryName);

        for (KV<String, Object> kv : params)
            query.setParameter(kv.getKey(), kv.getValue());

        if (firstResult > 0)
            query.setFirstResult(firstResult);
        if (maxResults > 0)
            query.setMaxResults(maxResults);

        return query.list();
    }

    /**
     * 
     * ??
     * 
     * @param queryName      : ??
     * @param params      : ?
     * @return            : ?
     * 
     */
    protected int namedUpdate(String queryName, Object... params) {
        Query query = getSession().getNamedQuery(queryName);

        for (int i = 0; i < params.length; i++)
            query.setParameter(i, params[i]);

        return query.executeUpdate();
    }

    /**
     * 
     * ??????
     * 
     * @param queryName      : ??
     * @param params      : ?????????
     * @return            : ?
     * 
     */
    protected int namedUpdate2(String queryName, KV<String, Object>... params) {
        Query query = getSession().getNamedQuery(queryName);

        for (KV<String, Object> kv : params)
            query.setParameter(kv.getKey(), kv.getValue());

        return query.executeUpdate();
    }

    /**
     * 
     * QBC
     * 
     * @param cls         : ?
     * @param criterions   : ?
     * @return            : 
     * 
     */
    protected <T> List<T> qbcQuery(Class<?> cls, Criterion... criterions) {
        return qbcQuery(0, 0, cls, criterions);
    }

    /**
     * 
     * ?QBC
     * 
     * @param cls         : ?
     * @param criterions   : ?
     * @return            : 
     * 
     */
    protected <T> List<T> qbcQuery(int firstResult, int maxResults, Class<?> cls, Criterion... criterions) {
        return qbcQuery(firstResult, maxResults, cls, (Order[]) null, criterions);
    }

    /**
     * 
     * QBC?
     * 
     * @param cls         : ?
     * @param orders      : ?
     * @param criterions   : ?
     * @return            : 
     * 
     */
    protected <T> List<T> qbcQuery(Class<?> cls, Order[] orders, Criterion... criterions) {
        return qbcQuery(0, 0, cls, (Order[]) orders, criterions);
    }

    /**
     * 
     * ?QBC?
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param cls         : ?
     * @param orders      : ?
     * @param criterions   : ?
     * @return            : 
     * 
     */
    protected <T> List<T> qbcQuery(int firstResult, int maxResults, Class<?> cls, Order[] orders,
            Criterion... criterions) {
        Criteria criteria = getSession().createCriteria(cls);

        for (Criterion c : criterions)
            criteria.add(c);

        if (orders != null) {
            for (Order o : orders)
                criteria.addOrder(o);
        }

        if (firstResult > 0)
            criteria.setFirstResult(firstResult);
        if (maxResults > 0)
            criteria.setMaxResults(maxResults);

        return criteria.list();
    }

    /**
     * 
     * SQL
     * 
     * @param sql      : SQL ?
     * @param params   : ?
     * @return         : 
     * 
     */
    protected <T> List<T> sqlQuery(String sql, Object... params) {
        return sqlQuery(0, 0, sql, params);
    }

    /**
     * 
     * SQL
     * 
     * @param sql      : SQL ?
     * @param clazz      : ?
     * @param params   : ?
     * @return         : 
     * 
     */
    protected <T> List<T> sqlQuery(String sql, Class<?> clazz, Object... params) {
        KV<String, Object>[] kvs = new KV[1];
        kvs[0] = new KV<String, Object>(null, clazz);

        return sqlQuery3(sql, kvs, params);
    }

    /**
     * 
     * ?SQL
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param sql         : SQL ?
     * @param params      : ?
     * @return            : 
     * 
     */
    protected <T> List<T> sqlQuery(int firstResult, int maxResults, String sql, Object... params) {
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);

        for (int i = 0; i < params.length; i++)
            sqlQuery.setParameter(i, params[i]);

        if (firstResult > 0)
            sqlQuery.setFirstResult(firstResult);
        if (maxResults > 0)
            sqlQuery.setMaxResults(maxResults);

        return sqlQuery.list();
    }

    /**
     * 
     * ?SQL
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param sql         : SQL ?
     * @param clazz         : ?
     * @param params      : ?
     * @return            : 
     * 
     */
    protected <T> List<T> sqlQuery(int firstResult, int maxResults, String sql, Class<?> clazz, Object... params) {
        KV<String, Object>[] kvs = new KV[1];
        kvs[0] = new KV<String, Object>(null, clazz);

        return sqlQuery4(firstResult, maxResults, sql, kvs, (KV<String, String>[]) null, params);
    }

    /**
     * 
     * SQLScalars?
     * 
     * @param sql      : SQL ?
     * @param scalars   : ??
     * @param params   : ?
     * @return         : 
     * 
     */
    protected <T> List<T> sqlQuery2(String sql, KV<String, Type>[] scalars, Object... params) {
        return sqlQuery2(0, 0, sql, (KV<String, Type>[]) scalars, params);
    }

    /**
     * 
     * ?SQLScalars?
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param sql         : SQL ?
     * @param scalars      : ??
     * @param params      : ?
     * @return            : 
     * 
     */
    protected <T> List<T> sqlQuery2(int firstResult, int maxResults, String sql, KV<String, Type>[] scalars,
            Object... params) {
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);

        for (int i = 0; i < params.length; i++)
            sqlQuery.setParameter(i, params[i]);

        if (scalars != null) {
            for (KV<String, Type> scalar : scalars) {
                String key = scalar.getKey();
                Type value = scalar.getValue();

                if (value != null)
                    sqlQuery.addScalar(key, value);
                else
                    sqlQuery.addScalar(key);
            }
        }

        if (firstResult > 0)
            sqlQuery.setFirstResult(firstResult);
        if (maxResults > 0)
            sqlQuery.setMaxResults(maxResults);

        return sqlQuery.list();
    }

    /**
     * 
     * SQL?Entities
     * 
     * @param entities   : ??
     * @param sql      : SQL ?
     * @param params   : ?
     * @return         : 
     * 
     */
    protected <T> List<T> sqlQuery3(String sql, KV<String, Object>[] entities, Object... params) {
        return sqlQuery3(0, 0, sql, (KV<String, Object>[]) entities, params);
    }

    /**
     * 
     * SQL?Entities
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param entities      : ??
     * @param sql         : SQL ?
     * @param params      : ?
     * @return            : 
     * 
     */
    protected <T> List<T> sqlQuery3(int firstResult, int maxResults, String sql, KV<String, Object>[] entities,
            Object... params) {
        return sqlQuery4(firstResult, maxResults, sql, (KV<String, Object>[]) entities, (KV<String, String>[]) null,
                params);
    }

    /**
     * 
     * SQL?EntitiesJoins
     * 
     * @param sql      : SQL ?
     * @param entities   : ??
     * @param joins      : ??
     * @param params   : ?
     * @return         : 
     * 
     */
    protected <T> List<T> sqlQuery4(String sql, KV<String, Object>[] entities, KV<String, String>[] joins,
            Object... params) {
        return sqlQuery4(0, 0, sql, (KV<String, Object>[]) entities, (KV<String, String>[]) joins, params);
    }

    /**
     * 
     * ?SQL?EntitiesJoins
     * 
     * @param firstResult   : 
     * @param maxResults   : 
     * @param sql         : SQL ?
     * @param entities      : ??
     * @param joins         : ??
     * @param params      : ?
     * @return            : 
     * 
     */
    protected <T> List<T> sqlQuery4(int firstResult, int maxResults, String sql, KV<String, Object>[] entities,
            KV<String, String>[] joins, Object... params) {
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);

        for (int i = 0; i < params.length; i++)
            sqlQuery.setParameter(i, params[i]);

        if (entities != null) {
            for (int i = 0; i < entities.length; i++) {
                KV<String, Object> entity = entities[i];

                String key = entity.getKey();
                Object value = entity.getValue();
                Class<?> v1 = (value instanceof Class) ? (Class<?>) value : null;
                String v2 = v1 == null ? (String) value : null;

                if (key == null || key.length() == 0) {
                    if (v1 != null)
                        sqlQuery.addEntity(v1);
                    else
                        sqlQuery.addEntity(v2);
                } else {
                    if (v1 != null)
                        sqlQuery.addEntity(key, v1);
                    else
                        sqlQuery.addEntity(key, v2);
                }
            }
        }

        if (joins != null) {
            for (KV<String, String> join : joins)
                sqlQuery.addJoin(join.getKey(), (String) join.getValue());
        }

        if (firstResult > 0)
            sqlQuery.setFirstResult(firstResult);
        if (maxResults > 0)
            sqlQuery.setMaxResults(maxResults);

        return sqlQuery.list();
    }

    /**
     * 
     * SQL
     * 
     * @param sql      : SQL ?
     * @param params   : ?
     * @return         : 
     * 
     */
    protected int sqlUpdate(String sql, Object... params) {
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);

        for (int i = 0; i < params.length; i++)
            sqlQuery.setParameter(i, params[i]);

        return sqlQuery.executeUpdate();
    }
}