<commit_before>package com.github.cnlinjie.infrastructure.dao.hibernate;

import com.github.cnlinjie.infrastructure.dao.PageParams;
import com.github.cnlinjie.infrastructure.dao.Page;
import com.github.cnlinjie.infrastructure.util.ReflectionUtils;
import com.github.cnlinjie.infrastructure.util.spring.Assert;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.internal.AbstractQueryImpl;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author Linjie
 * @date 2015/9/21.
 */
public abstract class HibernateSupportDao<T, PK extends Serializable> implements IHibernateCriteriaDao<T, PK>, IHibernateHqlDao<T, PK> {

    private boolean useCurrentSession = true;

    protected SessionFactory sessionFactory;

    private Session session;

    protected Class<T> entityClass;

    private static Logger logger = LoggerFactory.getLogger(HibernateSupportDao.class);

    public HibernateSupportDao() {
        this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass());
    }


    /**
     *
     * @return
     */
    public boolean isUseCurrentSession() {
        return useCurrentSession;
    }

    /**
     * 设置是否从当前线程读取Session，如果是 {false} 则，使用openSession
     * @param useCurrentSession
     */
    public void setUseCurrentSession(boolean useCurrentSession) {
        this.useCurrentSession = useCurrentSession;
    }

    /**
     * 设置Hibernate sessionFactory
     *
     * @param sessionFactory
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /**
     * 获取Hibernate SessionFactory
     *
     * @return {@link org.hibernate.SessionFactory}
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * 取得当前Session.
     *
     * @return {@link org.hibernate.Session}
     */
    public Session getSession() {
        if (null != session && session.isOpen()) {
            return session;
        }
        if (useCurrentSession) {
            this.session = sessionFactory.getCurrentSession();
        } else {<commit_msg>* BUG ，getCurrentSession() 是线程变量，所以每次都需要从getCurrentSession()里面取，不能判断不为空就 return 原来的，容易造成其他事务也在使用同一个session.<commit_after>package com.github.cnlinjie.infrastructure.dao.hibernate;

import com.github.cnlinjie.infrastructure.dao.PageParams;
import com.github.cnlinjie.infrastructure.dao.Page;
import com.github.cnlinjie.infrastructure.util.ReflectionUtils;
import com.github.cnlinjie.infrastructure.util.spring.Assert;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.internal.AbstractQueryImpl;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author Linjie
 * @date 2015/9/21.
 */
public abstract class HibernateSupportDao<T, PK extends Serializable> implements IHibernateCriteriaDao<T, PK>, IHibernateHqlDao<T, PK> {

    private boolean useCurrentSession = true;

    protected SessionFactory sessionFactory;

    private Session session;

    protected Class<T> entityClass;

    private static Logger logger = LoggerFactory.getLogger(HibernateSupportDao.class);

    public HibernateSupportDao() {
        this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass());
    }


    /**
     *
     * @return
     */
    public boolean isUseCurrentSession() {
        return useCurrentSession;
    }

    /**
     * 设置是否从当前线程读取Session，如果是 {false} 则，使用openSession
     * @param useCurrentSession
     */
    public void setUseCurrentSession(boolean useCurrentSession) {
        this.useCurrentSession = useCurrentSession;
    }

    /**
     * 设置Hibernate sessionFactory
     *
     * @param sessionFactory
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /**
     * 获取Hibernate SessionFactory
     *
     * @return {@link org.hibernate.SessionFactory}
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * 取得当前Session.
     *
     * @return {@link org.hibernate.Session}
     */
    public Session getSession() {
        if (!useCurrentSession && null != session && session.isOpen()) {
            return session;
        }
        if (useCurrentSession) {
            this.session = sessionFactory.getCurrentSession();
        } else {
{'fraction_non_alphanumeric': 0.09817262830482115, 'fraction_numerical': 0.0038880248833592537, 'mean_word_length': 3.4856146469049696, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4116151', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1529, 'n_words': 276}