package com.palmnest.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import javax.annotation.Resource;

public class CommDao {
	@Resource
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return this.sessionFactory;
	}

	public Session getSession() throws HibernateException {
		Session sess = getSessionFactory().getCurrentSession();
		if (sess == null) {
			sess = getSessionFactory().openSession();
		}
		return sess;
	}

	/**
	 * 获取某表的最大操作版本号
	 *
	 * @param tableName
	 * @return
	 */
	public static long getTabMaxOptVersion(String tableName) {
		String sql    = "select max(oprateversion) from " + tableName;
		String result = new CommDao().getSession().createSQLQuery(sql).uniqueResult() == null ? "" : new CommDao().getSession().createSQLQuery(sql).uniqueResult() + "";
		if (result == null || "".equals(result)) {
			return 0;
		}
		return Long.valueOf(result);
	}
}
