如题所示,对于这个问题,官网文档已给出答案,详见:
/** * 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) */ org.springframework.orm.hibernate3.support.HibernateDaoSupport#getSession()
Spring推荐我们使用HibernateTemplate,如有必要,则通过HibernateCallback来调用原生Session。
推荐原因,HibernateTemplate可以帮助我们进行资源管理和适当的异常转换!