package com.REP.DAO.base;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
public abstract class _BaseRootDAO {
public _BaseRootDAO () {}
public _BaseRootDAO (Session session) {
setSession(session);
}
protected static Map<String, SessionFactory> sessionFactoryMap;
protected SessionFactory sessionFactory;
protected Session session;
protected final static ThreadLocal<Session> currentSession = new ThreadLocal<Session>();
/**
* Return a new Session object that must be closed when the work has been completed.
* @return the active Session
*/
public Session getSession() {
return getSession(
getConfigurationFileName());
}
/**
* Return a new Session object that must be closed when the work has been completed.
* @param configFile the config file must match the meta attribute "config-file" in the hibernate mapping file
* @return the active Session
*/
protected Session getSession(String configFile) {
if (null != session && session.isOpen()) return session;
else if (null != sessionFactory) {
Session s = currentSession.get();
if (null == s || !s.isOpen()) {
s = sessionFactory.openSession();
currentSession.set(s);
}
return s;
}
else {
Session s = currentSession.get();
if (null == s || !s.isOpen()) {
s = getSessionFactory(configFile).openSession();
currentSession.set(s);
}
return s;
}
}
public void setSession (Session session) {
this.session = session;
}
/**
* Configure the session factory by reading hibernate config file
*/
public static void initialize () {
com.REP.DAO._RootDAO.initialize(
(String) null);
}
/**
* Configure the session factory by reading hibernate config file
* @param configFileName the name of the configuration file
*/
public static void initialize (String configFileName) {
com.REP.DAO._RootDAO.initialize(
configFileName,
com.REP.DAO._RootDAO.getNewConfiguration(
null));
}
public static void initialize (String configFileName, Configuration configuration) {
if (null != sessionFactoryMap && null != sessionFactoryMap.get(configFileName)) return;
else {
if (null == configFileName) {
configuration.configure();
com.REP.DAO._RootDAO.setSessionFactory(
null,
configuration.buildSessionFactory());
}
else {
configuration.configure(
configFileName);
com.REP.DAO._RootDAO.setSessionFactory(
configFileName,
configuration.buildSessionFactory());
}
}
}
/**
* Set the session factory
*/
public void setSessionFactory (SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
/**
* Set the session factory
*/
protected static void setSessionFactory (String configFileName, SessionFactory sf) {
if (null == configFileName) configFileName = "";
if (null == sessionFactoryMap) sessionFactoryMap = new HashMap<String, SessionFactory>();
sessionFactoryMap.put(
configFileName,
sf);
}
/**
* Return the SessionFactory that is to be used by these DAOs. Change this
* and implement your own strategy if you, for example, want to pull the SessionFactory
* from the JNDI tree.
*/
public SessionFactory getSessionFactory() {
if (null != sessionFactory) return sessionFactory;
else return getSessionFactory(
getConfigurationFileName());
}
public SessionFactory getSessionFactory(String configFileName) {
if (null == configFileName) configFileName = "";
if (null == sessionFactoryMap)
initialize(configFileName);
SessionFactory sf = (SessionFactory) sessionFactoryMap.get(configFileName);
if (null == sf)
throw new RuntimeException("The session factory for '" + configFileName + "' has not been initialized (or an error occured during initialization)");
else
return sf;
}
/**
* Close all sessions for the current thread
*/
public static void closeCurrentSession () {
Session s = currentSession.get();
if (null != s) {
if (s.isOpen()) s.close();
currentSession.set(null);
}
}
/**
* Close the session
*/
public void closeSession (Session session) {
if (null != session) session.close();
}
/**
* Begin the transaction related to the session
*/
public Transaction beginTransaction(Session s) {
return s.beginTransaction();
}
/**
* Commit the given transaction
*/
public void commitTransaction(Transaction t) {
t.commit();
}
/**
* Return a new Configuration to use. This is not a mistake and is meant
* to be overridden in the RootDAO if you want to do something different.
* The file name is passed in so you have that to access. The config file
* is read in the initialize method.
*/
public static Configuration getNewConfiguration (String configFileName) {
return new Configuration();
}
/**
* Return the name of the configuration file to be used with this DAO or null if default
*/
public String getConfigurationFileName () {
return null;
}
/**
* Return the specific Object class that will be used for class-specific
* implementation of this DAO.
* @return the reference Class
*/
protected abstract Class getReferenceClass();
/**
* Used by the base DAO classes but here for your modification
* Get object matching the given key and return it.
*/
protected Object get(Class refClass, Serializable key) {
Session s = null;
try {
s = getSession();
return get(refClass, key, s);
} finally {
closeSession(s);
}
}
/**
* Used by the base DAO classes but here for your modification
* Get object matching the given key and return it.
*/
protected Object get(Class refClass, Serializable key, Session s) {
return s.get(refClass, key);
}
/**
* Used by the base DAO classes but here for your modification
* Load object matching the given key and return it.
*/
protected Object load(Class refClass, Serializable key) {
Session s = null;
try {
s = getSession();
return load(refClass, key, s);
} finally {
closeSession(s);
}
}
/**
* Used by the base DAO classes but here for your modification
* Load object matching the given key and return it.
*/
protected Object load(Class refClass, Serializable key, Session s) {
return s.load(refClass, key);
}
/**
* Return all objects related to the implementation of this DAO with no filter.
*/
public java.util.List findAll () {
Session s = null;
try {
s = getSession();
return findAll(s);
}
finally {
closeSession(s);
}
}
/**
* Return all objects related to the implementation of this DAO with no filter.
* Use the session given.
* @param s the Session
*/
public java.util.List findAll (Session s) {
return findAll(s, getDefaultOrder());
}
/**
* Return all objects related to the implementation of this DAO with no filter.
*/
public java.util.List findAll (Order defaultOrder) {
Session s = null;
try {
s = getSession();
return findAll(s, defaultOrder);
}
finally {
closeSession(s);
}
}
/**
* Return all objects related to the implementation of this DAO with no filter.
* Use the session given.
* @param s the Session
*/
public java.util.List findAll (Session s, Order defaultOrder) {
Criteria crit = s.createCriteria(getReferenceClass());
if (null != defaultOrder) crit.addOrder(defaultOrder);
return crit.list();
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论

















收起资源包目录





































































































共 298 条
- 1
- 2
- 3
jaisokforron
- 粉丝: 6
- 资源: 10

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
最新资源
- 华为OD机试(..75.rar
- Oracle Capacity User Guide RELEASE 11i
- IT实践报告(大学生)
- 一款基于美格SLM130设计的智能安全帽参考设计
- Oracle Supply Chain Trading Connector User’s Guide Release 11.5
- Oracle Inventory Consigned Inventory From Supplier Process Guide
- Oracle Inventory Copy Inventory Organization Implementation Guid
- 一款基于龙尚M5700设计的智能定位安全帽参考设计
- 100+大数据可视化炫酷大屏Html5模板
- Oracle Supplier Scheduling User’s Guide, Release 11
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

- 1
- 2
前往页