📄 _baserootdao.java
字号:
package com.test.Hibernate.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 {
protected static Map sessionFactoryMap;
protected static SessionFactory sessionFactory;
protected static ThreadLocal mappedSessions;
protected static ThreadLocal sessions;
/**
* Configure the session factory by reading hibernate config file
*/
public static void initialize () throws HibernateException {
com.test.Hibernate.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) throws HibernateException {
com.test.Hibernate.dao._RootDAO.initialize(
configFileName,
com.test.Hibernate.dao._RootDAO.getNewConfiguration(
null));
}
public static void initialize (String configFileName, Configuration configuration) throws HibernateException {
if (null == configFileName && null != sessionFactory) return;
else if (null != sessionFactoryMap && null != sessionFactoryMap.get(configFileName)) return;
else {
if (null == configFileName) {
configuration.configure();
com.test.Hibernate.dao._RootDAO.setSessionFactory(
configuration.buildSessionFactory());
}
else {
configuration.configure(
configFileName);
com.test.Hibernate.dao._RootDAO.setSessionFactory(
configFileName,
configuration.buildSessionFactory());
}
}
}
/**
* Set the session factory
*/
protected static void setSessionFactory (SessionFactory sessionFactory) {
setSessionFactory(
(String) null,
sessionFactory);
}
/**
* Set the session factory
*/
protected static void setSessionFactory (String configFileName, SessionFactory sf) {
if (null == configFileName) {
sessionFactory = sf;
}
else {
if (null == sessionFactoryMap) sessionFactoryMap = new HashMap();
sessionFactoryMap.put(
configFileName,
sessionFactory);
}
}
/**
* 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.
*/
protected SessionFactory getSessionFactory() {
return getSessionFactory(
getConfigurationFileName());
}
protected SessionFactory getSessionFactory(String configFile) {
if (null == configFile) {
if (null == sessionFactory)
throw new RuntimeException("The session factory has not been initialized (or an error occured during initialization)");
else
return sessionFactory;
}
else {
if (null == sessionFactoryMap)
throw new RuntimeException("The session factory for '" + configFile + "' has not been initialized (or an error occured during initialization)");
else {
SessionFactory sf = (SessionFactory) sessionFactoryMap.get(configFile);
if (null == sf)
throw new RuntimeException("The session factory for '" + configFile + "' has not been initialized (or an error occured during initialization)");
else
return sf;
}
}
}
/**
* Return a new Session object that must be closed when the work has been completed.
* @return the active Session
*/
public Session getSession() throws HibernateException {
return getSession(
getConfigurationFileName(),
false);
}
/**
* Return a new Session object that must be closed when the work has been completed.
* @return the active Session
*/
public Session createNewSession() throws HibernateException {
return getSession(
getConfigurationFileName(),
true);
}
/**
* 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
*/
private Session getSession(String configFile, boolean createNew) throws HibernateException {
if (createNew) {
return getSessionFactory(configFile).openSession();
}
else {
if (null == configFile) {
if (null == sessions) sessions = new ThreadLocal();
Session session = (Session) sessions.get();
if (null == session || !session.isOpen()) {
session = getSessionFactory(null).openSession();
sessions.set(session);
}
return session;
}
else {
if (null == mappedSessions) mappedSessions = new ThreadLocal();
java.util.HashMap map = (java.util.HashMap) mappedSessions.get();
if (null == map) {
map = new HashMap(1);
mappedSessions.set(map);
}
Session session = (Session) map.get(configFile);
if (null == session || !session.isOpen()) {
session = getSessionFactory(configFile).openSession();
map.put(configFile, session);
}
return session;
}
}
}
/**
* Close all sessions for the current thread
*/
public static void closeCurrentThreadSessions () throws HibernateException {
if (null != sessions) {
Session session = (Session) sessions.get();
if (null != session && session.isOpen()) {
session.close();
}
}
if (null != mappedSessions) {
java.util.HashMap map = (java.util.HashMap) mappedSessions.get();
if (null != map) {
HibernateException thrownException = null;
Session session = null;
for (Iterator i=map.values().iterator(); i.hasNext(); ) {
session = (Session) i.next();
try {
if (null != session && session.isOpen()) {
session.close();
}
}
catch (HibernateException e) {
thrownException = e;
}
}
map.clear();
if (null != thrownException) throw thrownException;
}
}
}
/**
* Close the session
*/
public void closeSession (Session session) throws HibernateException {
if (null != session) session.close();
}
/**
* Begin the transaction related to the session
*/
public Transaction beginTransaction(Session s) throws HibernateException {
return s.beginTransaction();
}
/**
* Commit the given transaction
*/
public void commitTransaction(Transaction t) throws HibernateException {
t.commit();
}
/**
* Return a new Configuration to use
*/
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) throws HibernateException {
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) throws HibernateException {
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) throws HibernateException {
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) throws HibernateException {
return s.load(refClass, key);
}
/**
* Return all objects related to the implementation of this DAO with no filter.
*/
public java.util.List findAll () throws HibernateException {
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) throws HibernateException {
return findAll(s, getDefaultOrder());
}
/**
* Return all objects related to the implementation of this DAO with no filter.
*/
public java.util.List findAll (Order defaultOrder) throws HibernateException {
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) throws HibernateException {
Criteria crit = s.createCriteria(getReferenceClass());
if (null != defaultOrder) crit.addOrder(defaultOrder);
return crit.list();
}
/**
* Return all objects related to the implementation of this DAO with a filter.
* Use the session given.
* @param propName the name of the property to use for filtering
* @param filter the value of the filter
*/
protected java.util.List findFiltered (String propName, Object filter) throws HibernateException {
return findFiltered(propName, filter, getDefaultOrder());
}
/**
* Return all objects related to the implementation of this DAO with a filter.
* Use the session given.
* @param propName the name of the property to use for filtering
* @param filter the value of the filter
* @param orderProperty the name of the property used for ordering
*/
protected java.util.List findFiltered (String propName, Object filter, Order order) throws HibernateException {
Session s = null;
try {
s = getSession();
return findFiltered(s, propName, filter, getDefaultOrder());
}
finally {
closeSession(s);
}
}
/**
* Return all objects related to the implementation of this DAO with a filter.
* Use the session given.
* @param s the Session
* @param propName the name of the property to use for filtering
* @param filter the value of the filter
* @param orderProperty the name of the property used for ordering
*/
protected java.util.List findFiltered (Session s, String propName, Object filter, Order order) throws HibernateException {
Criteria crit = s.createCriteria(getReferenceClass());
crit.add(Expression.eq(propName, filter));
if (null != order) crit.addOrder(order);
return crit.list();
}
/**
* Obtain an instance of Query for a named query string defined in the mapping file.
* @param name the name of a query defined externally
* @return Query
*/
protected Query getNamedQuery(String name) throws HibernateException {
Session s = null;
try {
s = getSession();
return getNamedQuery(name, s);
} finally {
closeSession(s);
}
}
/**
* Obtain an instance of Query for a named query string defined in the mapping file.
* Use the session given.
* @param name the name of a query defined externally
* @param s the Session
* @return Query
*/
protected Query getNamedQuery(String name, Session s) throws HibernateException {
Query q = s.getNamedQuery(name);
return q;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -