package com.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.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.dao._RootDAO.initialize(
configFileName,
com.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.dao._RootDAO.setSessionFactory(
null,
configuration.buildSessionFactory());
}
else {
configuration.configure(
configFileName);
com.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();
}
/**
* Return all obj
没有合适的资源?快使用搜索试试~ 我知道了~
hibernate example
共153个文件
htm:31个
jpg:30个
class:23个
需积分: 10 14 下载量 139 浏览量
2009-02-15
23:45:34
上传
评论
收藏 5.12MB RAR 举报
温馨提示
hibernate example....................
资源详情
资源评论
资源推荐
收起资源包目录
hibernate example (153个子文件)
_BaseRootDAO.class 15KB
BaseAdminDAO.class 4KB
BaseAdmin.class 3KB
HibernateSessionFactory.class 2KB
HibernateSessionFactory.class 2KB
_BaseRootDAO$TransactionPointer.class 2KB
insertAdmin.class 2KB
_BaseRootDAO$ThreadRunner.class 2KB
AdminDAO.class 1KB
admin.class 1KB
_BaseRootDAO$4.class 898B
_BaseRootDAO$1.class 877B
_BaseRootDAO$2.class 848B
_BaseRootDAO$5.class 842B
_BaseRootDAO$3.class 842B
Time.class 771B
_BaseRootDAO$TransactionRunnable.class 594B
_BaseRootDAO$TransactionRunnableFailHandler.class 591B
hello.class 515B
Admin.class 456B
_RootDAO.class 442B
AdminDAO.class 425B
_BaseRootDAO$TransactionFailHandler.class 266B
.classpath 1KB
space.css 27KB
space.css 27KB
space.css 27KB
375071c4ced233a98226ac86.css 18KB
4b7001177acec0014a90a7be.css 17KB
d8854f2c8dbcfdec8a1399de.css 16KB
mods.css 7KB
mods.css 7KB
mods.css 7KB
image062.gif 11KB
image001.gif 663B
image039.gif 591B
在Eclipse上创建Hibernate实例_Java.htm 62KB
index(16).htm 60KB
index(8).htm 60KB
index(1).htm 60KB
index(24).htm 60KB
index(19).htm 60KB
index(22).htm 60KB
index(7).htm 60KB
index(25).htm 60KB
index(15).htm 60KB
index(11).htm 60KB
index(12).htm 60KB
index(21).htm 60KB
index(6).htm 60KB
index(2).htm 60KB
index(20).htm 60KB
index(18).htm 60KB
index(27).htm 60KB
index(10).htm 60KB
index(5).htm 60KB
index(4).htm 60KB
index.htm 60KB
index(3).htm 60KB
index(14).htm 60KB
index(17).htm 60KB
index(9).htm 60KB
index(26).htm 60KB
index(23).htm 60KB
index(13).htm 60KB
利用Eclipse开发Hibernate应用程序_自由呼吸.htm 36KB
我的第一个hibernate例子_JAVA咖啡屋--逝去的大四.htm 33KB
hibernate3.jar 1.63MB
xerces-2.6.2.jar 966KB
mysql-connector-java.jar 436KB
antlr-2.7.6rc1.jar 425KB
log4j-1.2.11.jar 305KB
dom4j-1.6.1.jar 257KB
cglib-2.1.3.jar 244KB
jaxen-1.1-beta-7.jar 194KB
commons-collections-2.1.1.jar 169KB
xml-apis.jar 117KB
jaas.jar 99KB
ehcache-1.1.jar 41KB
commons-logging-1.0.4.jar 33KB
asm.jar 25KB
asm-attrs.jar 16KB
jta.jar 8KB
jdbc2_0-stdext.jar 6KB
_BaseRootDAO.java 25KB
BaseAdminDAO.java 7KB
HibernateSessionFactory.java 3KB
HibernateSessionFactory.java 3KB
BaseAdmin.java 3KB
AdminDAO.java 2KB
insertAdmin.java 1KB
_RootDAO.java 973B
admin.java 834B
AdminDAO.java 805B
Admin.java 353B
Time.java 293B
hello.java 120B
image064.jpg 23KB
image058.jpg 22KB
image042.jpg 20KB
共 153 条
- 1
- 2
ysgml333
- 粉丝: 1
- 资源: 89
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0