package com.jspdev.biyesheji.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.jspdev.biyesheji.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.jspdev.biyesheji.dao._RootDAO.initialize(
configFileName,
com.jspdev.biyesheji.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.jspdev.biyesheji.dao._RootDAO.setSessionFactory(
null,
configuration.buildSessionFactory());
}
else {
configuration.configure(
configFileName);
com.jspdev.biyesheji.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 (nul
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
struts 2Bhibernate实现的教务选课信息管理系统.rar (336个子文件)
_BaseRootDAO.class 15KB
Student.class 6KB
BaseStudent.class 5KB
Teacher.class 5KB
Sc.class 5KB
BaseStudentDAO.class 4KB
BaseTeacherDAO.class 4KB
BaseMessageDAO.class 4KB
BaseCourseDAO.class 4KB
BaseSc.class 4KB
Grade.class 4KB
BaseGradeDAO.class 4KB
BaseCourse.class 4KB
BaseScDAO.class 4KB
BaseNoteDAO.class 4KB
Course.class 4KB
BaseTeacher.class 4KB
Message.class 4KB
TeachersQueryAction.class 4KB
BaseMessage.class 3KB
BaseGrade.class 3KB
StudentsQueryAction.class 3KB
GradeQueryAction.class 3KB
StuInfoForm.class 3KB
addTeacherServlet.class 3KB
addStudentServlet.class 3KB
CoursesQueryAction.class 3KB
GradeAction.class 3KB
BaseNote.class 2KB
HibernateSessionFactory.class 2KB
LoginAction.class 2KB
courseInfoServlet.class 2KB
ScAction.class 2KB
_BaseRootDAO$TransactionPointer.class 2KB
f_tScServlet.class 2KB
CourseForm.class 2KB
t_fScServlet.class 2KB
_BaseRootDAO$ThreadRunner.class 2KB
TeaInfoForm.class 2KB
BaseGradePK.class 2KB
PasswordForm.class 2KB
PageControlServlet.class 2KB
deleteMessageServlet.class 2KB
GradeForm.class 2KB
CourseAction.class 2KB
changeGradeServlet.class 2KB
MessageForm.class 2KB
SignonFilter.class 2KB
PasswordAction.class 2KB
MessageAction.class 2KB
Note.class 2KB
StudentsQueryForm.class 2KB
deleteTeacherServlet.class 2KB
TeachersQueryForm.class 2KB
EncodingFilter.class 2KB
CancelScAction.class 1KB
GradeQueryForm.class 1KB
LoginForm.class 1KB
StuInfoAction.class 1KB
TeaInfoAction.class 1KB
CancelScForm.class 1KB
DeleteTeachersAction.class 1KB
DeleteStudentsAction.class 1KB
DeleteCoursesAction.class 1KB
CoursesQueryForm.class 1KB
NoteAction.class 1KB
ScForm.class 1KB
deleteStudentServlet.class 1KB
deleteCourseServlet.class 1KB
NoteForm.class 1KB
DeleteTeachersForm.class 1KB
DeleteStudentsForm.class 1KB
DeleteCoursesForm.class 1KB
_BaseRootDAO$4.class 1017B
_BaseRootDAO$1.class 996B
Page.class 994B
_BaseRootDAO$2.class 967B
_BaseRootDAO$3.class 961B
_BaseRootDAO$5.class 961B
HibernateUtil.class 874B
_BaseRootDAO$TransactionRunnableFailHandler.class 710B
_BaseRootDAO$TransactionRunnable.class 679B
GradePK.class 628B
GradeDAO.class 543B
MessageDAO.class 529B
TeacherDAO.class 529B
StudentDAO.class 529B
CourseDAO.class 524B
TeacherDAO.class 519B
StudentDAO.class 519B
GradeDAO.class 519B
NoteDAO.class 514B
CourseDAO.class 513B
ScDAO.class 504B
NoteDAO.class 501B
_RootDAO.class 493B
MessageDAO.class 337B
ScDAO.class 317B
_BaseRootDAO$TransactionFailHandler.class 300B
.classpath 2KB
共 336 条
- 1
- 2
- 3
- 4
资源评论
zzzzl333
- 粉丝: 676
- 资源: 7万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功