Implementing Spring-based DAOs without callbacks

~


Implementing Spring-based DAOs without callbacks_第1张图片
 

附上源码更清楚(部分):

package org.springframework.orm.hibernate3.support;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.support.DaoSupport;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;


public abstract class HibernateDaoSupport extends DaoSupport {

 private HibernateTemplate hibernateTemplate;

……

 /**
  * Obtain a Hibernate Session, either from the current transaction or
  * a new one. The latter is only allowed if the
  * {@link org.springframework.orm.hibernate3.HibernateTemplate#setAllowCreate "allowCreate"}
  * setting of this bean's {@link #setHibernateTemplate HibernateTemplate} is "true".
  * <p><b>Note that this is not meant to be invoked from HibernateTemplate code
  * but rather just in plain Hibernate code.</b> Either rely on a thread-bound
  * Session or use it in combination with {@link #releaseSession}.
  * <p>In general, it is recommended to use HibernateTemplate, either with
  * the provided convenience operations or with a custom HibernateCallback
  * that provides you with a Session to work on. HibernateTemplate will care
  * for all resource management and for proper exception conversion.
  * @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.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean)
  */
 protected final Session getSession()
     throws DataAccessResourceFailureException, IllegalStateException {

  return getSession(this.hibernateTemplate.isAllowCreate());
 }

 /**
  * Obtain a Hibernate Session, either from the current transaction or
  * a new one. The latter is only allowed if "allowCreate" is true.
  * <p><b>Note that this is not meant to be invoked from HibernateTemplate code
  * but rather just in plain Hibernate code.</b> Either rely on a thread-bound
  * Session or use it in combination with {@link #releaseSession}.
  * <p>In general, it is recommended to use
  * {@link #getHibernateTemplate() HibernateTemplate}, either with
  * the provided convenience operations or with a custom
  * {@link org.springframework.orm.hibernate3.HibernateCallback} that
  * provides you with a Session to work on. HibernateTemplate will care
  * for all resource management and for proper exception conversion.
  * @param allowCreate if a non-transactional Session should be created when no
  * transactional Session can be found for the current thread
  * @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.hibernate3.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()));
 }

……
}

 

而 HibernateTemplate 中,allowCreate 的默认值是 true,抄录部分源码如下:
public class HibernateTemplate extends HibernateAccessor implements HibernateOperations {

 private boolean allowCreate = true;

 …… 

}

你可能感兴趣的:(DAO,spring,thread,Hibernate,orm)