📄 hibernatedaosupport.java
字号:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.hibernate.support;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.CleanupFailureDataAccessException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.SessionFactoryUtils;
/**
* Convenient super class for Hibernate data access objects.
*
* <p>Requires a SessionFactory to be set, providing a HibernateTemplate
* based on it to subclasses. Can alternatively be initialized directly via a
* HibernateTemplate, to reuse the latter's settings like SessionFactory,
* flush mode, exception translator, etc.
*
* <p>This base class is mainly intended for HibernateTemplate usage
* but can also be used when working with SessionFactoryUtils directly,
* e.g. in combination with HibernateInterceptor-managed Sessions.
* Convenience getSession and closeSessionIfNecessary methods are provided
* for that usage.
*
* @author Juergen Hoeller
* @since 28.07.2003
* @see #setSessionFactory
* @see #setHibernateTemplate
* @see org.springframework.orm.hibernate.HibernateTemplate
* @see org.springframework.orm.hibernate.HibernateInterceptor
*/
public abstract class HibernateDaoSupport implements InitializingBean {
protected final Log logger = LogFactory.getLog(getClass());
private HibernateTemplate hibernateTemplate;
/**
* Set the Hibernate SessionFactory to be used by this DAO.
*/
public final void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
/**
* Return the Hibernate SessionFactory used by this DAO.
*/
protected final SessionFactory getSessionFactory() {
return hibernateTemplate.getSessionFactory();
}
/**
* Set the HibernateTemplate for this DAO explicitly,
* as an alternative to specifying a SessionFactory.
*/
public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/**
* Return the HibernateTemplate for this DAO,
* pre-initialized with the SessionFactory or set explicitly.
*/
protected final HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public final void afterPropertiesSet() throws Exception {
if (this.hibernateTemplate == null) {
throw new IllegalArgumentException("sessionFactory or hibernateTemplate is required");
}
initDao();
}
/**
* Subclasses can override this for custom initialization behavior.
* Gets called after population of this instance's bean properties.
* @throws Exception if initialization fails
*/
protected void initDao() throws Exception {
}
/**
* Get a Hibernate Session, either from the current transaction or
* a new one. The latter is only allowed if the "allowCreate" setting
* of this bean's HibernateTemplate is true.
* @return the Hibernate Session
* @throws DataAccessResourceFailureException if the Session couldn't be created
* @throws IllegalStateException if no thread-bound Session found and allowCreate false
* @see org.springframework.orm.hibernate.HibernateTemplate
*/
protected final Session getSession()
throws DataAccessResourceFailureException, IllegalStateException {
return getSession(this.hibernateTemplate.isAllowCreate());
}
/**
* Get a Hibernate Session, either from the current transaction or
* a new one. The latter is only allowed if "allowCreate" is true.
* @param allowCreate if a new Session should be created if no thread-bound found
* @return the Hibernate Session
* @throws DataAccessResourceFailureException if the Session couldn't be created
* @throws IllegalStateException if no thread-bound Session found and allowCreate false
* @see org.springframework.orm.hibernate.SessionFactoryUtils#getSession(SessionFactory, boolean)
*/
protected final Session getSession(boolean allowCreate)
throws DataAccessResourceFailureException, IllegalStateException {
return (!allowCreate ?
SessionFactoryUtils.getSession(getSessionFactory(), false) :
SessionFactoryUtils.getSession(getSessionFactory(),
this.hibernateTemplate.getEntityInterceptor(),
this.hibernateTemplate.getJdbcExceptionTranslator()));
}
/**
* Convert the given HibernateException to an appropriate exception from
* the org.springframework.dao hierarchy. Will automatically detect
* wrapped SQLExceptions and convert them accordingly.
* <p>Delegates to the convertHibernateAccessException method of this
* DAO's HibernateTemplate.
* @param ex HibernateException that occured
* @return the corresponding DataAccessException instance
* @see #setHibernateTemplate
* @see org.springframework.orm.hibernate.HibernateTemplate#convertHibernateAccessException
*/
protected final DataAccessException convertHibernateAccessException(HibernateException ex) {
return this.hibernateTemplate.convertHibernateAccessException(ex);
}
/**
* Close the given Hibernate Session if necessary, created via this bean's
* SessionFactory, if it isn't bound to the thread.
* @param session Session to close
* @throws DataAccessResourceFailureException if the Session couldn't be closed
* @see org.springframework.orm.hibernate.SessionFactoryUtils#closeSessionIfNecessary
*/
protected final void closeSessionIfNecessary(Session session)
throws CleanupFailureDataAccessException {
SessionFactoryUtils.closeSessionIfNecessary(session, getSessionFactory());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -