org.workin.persistence.hibernate.v4.dao.Hibernate4DaoSupport.java Source code

Java tutorial

Introduction

Here is the source code for org.workin.persistence.hibernate.v4.dao.Hibernate4DaoSupport.java

Source

/*
 * Copyright 2015 the original author or authors.
 *
 * 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.
 *  
 * Create Date : 2015-2-2
 */

package org.workin.persistence.hibernate.v4.dao;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.workin.commons.util.ArrayUtils;
import org.workin.commons.util.MapUtils;
import org.workin.commons.util.StringUtils;
import org.workin.persistence.hibernate.v4.Hibernate4CacheConfiguration;

/**
 * @description Hibernate4 DAO?
 * @author  <a href="mailto:code727@gmail.com">?</a>
 * @version 1.0
 */
public abstract class Hibernate4DaoSupport implements InitializingBean {

    @Autowired
    private SessionFactory sessionFactory;

    /** ? */
    private Hibernate4CacheConfiguration cacheConfiguration;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Hibernate4CacheConfiguration getCacheConfiguration() {
        return cacheConfiguration;
    }

    public void setCacheConfiguration(Hibernate4CacheConfiguration cacheConfiguration) {
        this.cacheConfiguration = cacheConfiguration;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (sessionFactory == null)
            throw new BeanCreationException(
                    "SessionFactory object can not be null, please inject to spring container.");
    }

    /**
     * @description ?
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @return
     */
    public Session openSession() {
        return this.sessionFactory.openSession();
    }

    /**
     * @description ???
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @return
     */
    public Session getCurrentSession() {
        Session session;
        try {
            session = this.sessionFactory.getCurrentSession();
        } catch (HibernateException e) {
            // ?
            session = openSession();
        }
        return session;
    }

    /**
     * @description ??
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param entityClass
     * @return
     */
    protected ClassMetadata getClassMetadata(Class<?> entityClass) {
        return this.sessionFactory.getClassMetadata(entityClass);
    }

    /**
     * @description ???
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param entityClass
     * @return
     */
    protected String entityName(Class<?> entityClass) {
        return getClassMetadata(entityClass).getEntityName();
    }

    /**
     * @description ???
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param entityClass
     * @return
     */
    protected String getTableName(Class<?> entityClass) {
        return ((AbstractEntityPersister) getClassMetadata(entityClass)).getTableName();
    }

    /**
     * @description ???
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param clazz
     * @return
     */
    protected String getPrimaryKeyName(Class<?> entityClass) {
        return getClassMetadata(entityClass).getIdentifierPropertyName();
    }

    /**
     * @description ?
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param criteria ?
     * @param start ?
     * @param maxRows 
     */
    protected void setOffsetCriteria(Criteria criteria, int start, int maxRows) {
        if (start >= 0)
            criteria.setFirstResult(start);
        if (maxRows > 0)
            criteria.setMaxResults(maxRows);
    }

    /**
     * @description ?
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param query ?
     * @param start ?
     * @param maxRows 
     */
    protected void setOffsetQuery(Query query, int start, int maxRows) {
        if (start >= 0)
            query.setFirstResult(start);
        if (maxRows > 0)
            query.setMaxResults(maxRows);
    }

    /**
     * @description ????
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param query ?
     * @param values ??
     */
    protected void setQueryParameters(Query query, Object[] values) {
        if (ArrayUtils.isNotEmpty(values)) {
            for (int i = 0; i < values.length; i++)
                query.setParameter(i, values[i]);
        }
    }

    /**
     * @description ????
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param query ?
     * @param values ??
     * @param start ?
     * @param maxRows 
     */
    protected void setQueryParameters(Query query, Object[] values, int start, int maxRows) {
        setQueryParameters(query, values);
        setOffsetQuery(query, start, maxRows);
    }

    /**
     * @description ????
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param query
     * @param paramName ???
     * @param paramValue ?
     */
    protected void setQueryNamedParameter(Query query, String paramName, Object paramValue) {
        if (StringUtils.isNotBlank(paramName))
            query.setParameter(paramName, paramValue);
    }

    /**
     * @description ????
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param query ?
     * @param paramName ???
     * @param paramValue ?
     * @param start ?
     * @param maxRows 
     */
    protected void setQueryNamedParameter(Query query, String paramName, Object paramValue, int start,
            int maxRows) {
        setQueryNamedParameter(query, paramName, paramValue);
        setOffsetQuery(query, start, maxRows);
    }

    /**
     * @description ????
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param query ?
     * @param paramMap ???-
     */
    protected void setQueryNamedParameters(Query query, Map<String, ?> paramMap) {
        if (MapUtils.isNotEmpty(paramMap)) {
            Iterator<?> iterator = paramMap.entrySet().iterator();
            String name;
            while (iterator.hasNext()) {
                Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
                name = (String) entry.getKey();
                if (StringUtils.isNotBlank(name))
                    query.setParameter(name, entry.getValue());
            }
        }
    }

    /**
     * @description ????
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param query ?
     * @param paramMap ???-
     * @param start ?
     * @param maxRows 
     */
    protected void setQueryNamedParameters(Query query, Map<String, ?> paramMap, int start, int maxRows) {
        setQueryNamedParameters(query, paramMap);
        setOffsetQuery(query, start, maxRows);
    }

    /**
     * @description ??Criteria
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param criteria
     */
    protected void prepareCriteria(Criteria criteria) {
        if (this.cacheConfiguration != null) {
            if (this.cacheConfiguration.isCacheQueries()) {
                criteria.setCacheable(true);
                if (StringUtils.isNotBlank(this.cacheConfiguration.getQueryCacheRegion())) {
                    criteria.setCacheRegion(this.cacheConfiguration.getQueryCacheRegion());
                }
            }

            if (this.cacheConfiguration.getFetchSize() > 0)
                criteria.setFetchSize(this.cacheConfiguration.getFetchSize());

            if (this.cacheConfiguration.getMaxResults() > 0)
                criteria.setMaxResults(this.cacheConfiguration.getMaxResults());
        }
    }

    /**
     * @description ??Query
     * @author <a href="mailto:code727@gmail.com">?</a> 
     * @param query
     */
    protected void prepareQuery(Query query) {
        if (this.cacheConfiguration != null) {
            if (this.cacheConfiguration.isCacheQueries()) {
                query.setCacheable(true);
                if (StringUtils.isNotBlank(this.cacheConfiguration.getQueryCacheRegion())) {
                    query.setCacheRegion(this.cacheConfiguration.getQueryCacheRegion());
                }
            }

            if (this.cacheConfiguration.getFetchSize() > 0)
                query.setFetchSize(this.cacheConfiguration.getFetchSize());

            if (this.cacheConfiguration.getMaxResults() > 0)
                query.setMaxResults(this.cacheConfiguration.getMaxResults());
        }
    }

}