_baserootdao.java
来自「一个购房管理系统,JSF+Hibernate+Mssql2」· Java 代码 · 共 1,023 行 · 第 1/2 页
JAVA
1,023 行
package com.housesale.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 {
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.housesale.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) {
com.housesale.hibernate.dao._RootDAO.initialize(configFileName,
com.housesale.hibernate.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.housesale.hibernate.dao._RootDAO.setSessionFactory(null,
configuration.buildSessionFactory());
} else {
configuration.configure(configFileName);
com.housesale.hibernate.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 {
}
}
/**
* 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 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 Criteria findFiltered(String propName, Object filter) {
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 Criteria findFiltered(String propName, Object filter, Order order) {
Session s = null;
try {
s = getSession();
return findFiltered(s, propName, filter, order);
} 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 Criteria findFiltered(Session s, String propName, Object filter,
Order order) {
Criteria crit = s.createCriteria(getReferenceClass());
crit.add(Expression.eq(propName, filter));
if (null != order)
crit.addOrder(order);
return crit;
}
/**
* 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) {
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) {
Query q = s.getNamedQuery(name);
return q;
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file.
*
* @param name
* the name of a query defined externally
* @param param
* the first parameter to set
* @return Query
*/
protected Query getNamedQuery(String name, Serializable param) {
Session s = null;
try {
s = getSession();
return getNamedQuery(name, param, 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 param
* the first parameter to set
* @param s
* the Session
* @return Query
*/
protected Query getNamedQuery(String name, Serializable param, Session s) {
Query q = s.getNamedQuery(name);
q.setParameter(0, param);
return q;
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file. Use the parameters given.
*
* @param name
* the name of a query defined externally
* @param params
* the parameter array
* @return Query
*/
protected Query getNamedQuery(String name, Serializable[] params) {
Session s = null;
try {
s = getSession();
return getNamedQuery(name, params, s);
} finally {
closeSession(s);
}
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file. Use the parameters given and the Session given.
*
* @param name
* the name of a query defined externally
* @param params
* the parameter array
* @s the Session
* @return Query
*/
protected Query getNamedQuery(String name, Serializable[] params, Session s) {
Query q = s.getNamedQuery(name);
if (null != params) {
for (int i = 0; i < params.length; i++) {
q.setParameter(i, params[i]);
}
}
return q;
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file. Use the parameters given.
*
* @param name
* the name of a query defined externally
* @param params
* the parameter Map
* @return Query
*/
protected Query getNamedQuery(String name, Map params) {
Session s = null;
try {
s = getSession();
return getNamedQuery(name, params, s);
} finally {
closeSession(s);
}
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file. Use the parameters given and the Session given.
*
* @param name
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?