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();
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
图书光盘中的代码部份,由于上传容量的限制,没有上传光盘中的视频. 张元亮编著的《布局JavaEE企业级开发:寻觅框架和开发模式的完美整合(附光盘)》可以帮助读者精心布 局JavaEE企业级开发,并以此寻觅出框架与开发模式完美整合的真谛。 《布局JavaEE企业级开发:寻觅框架和开发模式的完美整合(附光盘)》内容新颖、知识全面、讲解详细。 全书分为18章,内容都采用了理论加实践的讲述方法,每个实例先提出制作思路及包含的知识点,然后力 求用最通俗的语言将高深的知识阐述出来。 本书适合Java各个级别的程序员、研发人员及在职程序员阅读,也可以作为培训学校和大专院校相关专业 的教学用书。
资源推荐
资源详情
资源评论
收起资源包目录
布局JavaEE企业级开发_光盘_part1 (144个子文件)
TxAspect.class 3KB
LogAspect.class 2KB
Hello.class 2KB
LoginBean.class 1KB
Hello.class 600B
.classpath 5KB
.classpath 841B
.classpath 232B
org.eclipse.wst.common.component 463B
org.eclipse.wst.jsdt.ui.superType.container 49B
index.html 238B
wlfullclient.jar 54.06MB
jboss-aop-client.jar 1.28MB
jboss-metadata.jar 1.28MB
jboss-remoting.jar 1.08MB
jsf-impl.jar 823KB
javassist.jar 589KB
jboss-messaging-client.jar 564KB
jboss-common-core.jar 537KB
jsf-api.jar 353KB
trove.jar 353KB
concurrent.jar 250KB
jbosssx-client.jar 189KB
jboss-ejb3-core-client.jar 177KB
jboss-mdr.jar 175KB
jboss-javaee.jar 172KB
log4j-boot.jar 125KB
jboss-serialization.jar 123KB
jboss-security-spi.jar 70KB
jboss-integration.jar 64KB
jbosssx-as-client.jar 62KB
jboss-ejb3-proxy-impl-client.jar 57KB
jnp-client.jar 51KB
jboss-ejb3-security-client.jar 45KB
jboss-ejb3-ext-api-impl.jar 44KB
jboss-ejb3-common-client.jar 23KB
jstl.jar 20KB
jboss-ejb3-ext-api.jar 20KB
jboss-ejb3-proxy-clustered-client.jar 13KB
jboss-aspect-jdk50-client.jar 13KB
jboss-logging-spi.jar 13KB
CatService.jar 3KB
jboss-ejb3-proxy-spi-client.jar 2KB
jbossall-client.jar 1KB
_BaseRootDAO.java 25KB
fa.java 11KB
BaseEmployeeDAO.java 7KB
BaseManagerDAO.java 7KB
BaseAccountDAO.java 7KB
ManagerOperateAction.java 6KB
EmployeeServiceImpl.java 6KB
peizhi.java 5KB
EmployeeOperateAction.java 5KB
EmployeeRegistAction.java 3KB
BaseEmployee.java 3KB
ManagerServiceImpl.java 3KB
BaseAccount.java 3KB
BaseManager.java 3KB
shouqu.java 2KB
shou.java 2KB
QuizBean.java 2KB
EmployeeServiceTest.java 2KB
IEmployeeDAO.java 2KB
IManagerDAO.java 2KB
IAccountDAO.java 2KB
first.java 2KB
EjbClient.java 2KB
EmployeeDAOTest.java 2KB
fasong.java 2KB
fa.java 2KB
TxAspect.java 1KB
AccountDAO.java 1KB
Hello.java 1KB
IEmployeeService.java 1KB
ManagerDAO.java 1KB
EncodingProcessor.java 1KB
IManagerService.java 1KB
EmployeeDAO.java 1001B
_RootDAO.java 985B
AccoutDAOTest.java 887B
Person.java 877B
LoginBean.java 826B
LogoutAction.java 730B
CatServiceBean.java 612B
Manager.java 594B
Employee.java 586B
Account.java 562B
Cat.java 545B
Problem.java 479B
LogAspect.java 293B
Hello.java 281B
HelloBean.java 263B
TxAspect.java 236B
CatService.java 159B
Hello.java 126B
Hello.java 117B
EmployeeNotExistException.java 95B
AccountNotExistException.java 94B
AccountIsExistException.java 93B
EmployeeBeUsedException.java 93B
共 144 条
- 1
- 2
资源评论
- myhcl00112013-11-25非常的有价值,我要认真的学习呀
BraveExplorer
- 粉丝: 4
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功