/**
* 重写Spring的HibernateDaoSupport,使用注解完成SessionFactory的注入。
*/
package org.cric.dao.impl;
import javax.annotation.Resource;
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.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
public class MyHibernateDaoSupport {
@Resource private SessionFactory sessionFactory;
private HibernateTemplate hibernateTemplate;
/**
* Set the Hibernate SessionFactory to be used by this DAO. Will
* automatically create a HibernateTemplate for the given SessionFactory.
*
* @see #createHibernateTemplate
* @see #setHibernateTemplate
*/
public final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null
|| sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
}
/**
* Create a HibernateTemplate for the given SessionFactory. Only invoked if
* populating the DAO with a SessionFactory reference!
* <p>
* Can be overridden in subclasses to provide a HibernateTemplate instance
* with different configuration, or a custom HibernateTemplate subclass.
*
* @param sessionFactory
* the Hibernate SessionFactory to create a HibernateTemplate for
* @return the new HibernateTemplate instance
* @see #setSessionFactory
*/
protected HibernateTemplate createHibernateTemplate(
SessionFactory sessionFactory) {
return new HibernateTemplate(sessionFactory);
}
/**
* Return the Hibernate SessionFactory used by this DAO.
*/
public final SessionFactory getSessionFactory() {
return (this.hibernateTemplate != null ? this.hibernateTemplate
.getSessionFactory() : null);
}
/**
* Set the HibernateTemplate for this DAO explicitly, as an alternative to
* specifying a SessionFactory.
*
* @see #setSessionFactory
*/
public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/**
* Return the HibernateTemplate for this DAO, pre-initialized with the
* SessionFactory or set explicitly.
* <p>
* <b>Note: The returned HibernateTemplate is a shared instance.</b> You
* may introspect its configuration, but not modify the configuration (other
* than from within an {@link #initDao} implementation). Consider creating a
* custom HibernateTemplate instance via
* <code>new HibernateTemplate(getSessionFactory())</code>, in which case
* you're allowed to customize the settings on the resulting instance.
*/
public final HibernateTemplate getHibernateTemplate() {
this.setSessionFactory(sessionFactory);
return this.hibernateTemplate;
}
protected final void checkDaoConfig() {
if (this.hibernateTemplate == null) {
throw new IllegalArgumentException(
"'sessionFactory' or 'hibernateTemplate' is required");
}
}
/**
* 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 {
this.setSessionFactory(sessionFactory);
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()));
}
/**
* Convert the given HibernateException to an appropriate exception from the
* <code>org.springframework.dao</code> hierarchy. Will automatically
* detect wrapped SQLExceptions and convert them accordingly.
* <p>
* Delegates to the
* {@link org.springframework.orm.hibernate3.HibernateTemplate#convertHibernateAccessException}
* method of this DAO's HibernateTemplate.
* <p>
* Typically used in plain Hibernate code, in combination with
* {@link #getSession} and {@link #releaseSession}.
*
* @param ex
* HibernateException that occured
* @return the corresponding DataAccessException instance
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#convertHibernateAccessException
*/
protected final DataAccessException convertHibernateAccessException(
HibernateException ex) {
return this.hibernateTemplate.convertHibernateAccessException(ex);
}
/**
* Close the given Hibernate Session, created via this DAO's SessionFactory,
* if it isn't bound to the thread (i.e. isn't a transactional Session).
* <p>
* Typically used in plain Hibernate code, in combination with
* {@link #getSession} and {@link #convertHibernateAccessException}.
*
* @param session
* the Session to close
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#releaseSession
*/
protected final void releaseSession(Session session) {
SessionFactoryUtils.releaseSession(session, getSessionFactory());
}
}
分享到:
相关推荐
在Java开发领域,尤其是Spring框架的应用中,`HibernateDaoSupport`和`@Autowired`是两个非常重要的概念。它们分别代表了Hibernate对DAO层的支持以及Spring框架的依赖注入机制。接下来,我们将深入探讨这两个知识点...
《Spring框架下HibernateDaoSupport的深度解析与应用》 在Java企业级开发中,Spring框架以其优秀的IoC(控制反转)和AOP(面向切面编程)特性,成为了事实上的标准。而Hibernate作为主流的对象关系映射(ORM)工具...
### hibernateDaoSupport类的运用实例 #### 一、引言 `hibernateDaoSupport`是Spring框架中提供的一种支持Hibernate操作的基类。它主要用于简化Hibernate与Spring集成过程中的编码工作,使得开发人员能够更加专注于...
NULL 博文链接:https://wxinpeng.iteye.com/blog/1162157
根据给定的信息,我们可以深入探讨Spring框架中与Hibernate集成的相关知识点,特别关注“HibernateDaoSupport”类及其在Spring环境中的应用。以下是对标题、描述以及部分文件内容的详细解析: ### 一、Spring与...
HibernateDaoSupport 类的jar HibernateDao 的jar
Spring hibernate3. HibernateDaoSupport 源码
SSH整合(其中dao用extends HibernateDaoSupport方式)总结【图解】
本示例主要介绍如何实现Spring Boot 2.0多数据源的集成,并结合Hibernate进行配置,特别是在DAO层使用`HibernateDaoSupport`进行操作,而非使用JPA(Java Persistence API)。 首先,让我们了解Spring Boot 2.0的...
`HibernateDaoSupport`是Spring框架中为Hibernate提供的一个辅助类,用于简化DAO(数据访问对象)层的开发。本文将深入探讨`HibernateDaoSupport`的二次封装,以及如何通过封装来实现快速的统计、查询、修改和删除...
### HibernateDaoSupport 分页实现详解 #### 一、概述 在Java开发中,尤其是在Web应用领域,对数据库的高效查询及数据展示是非常重要的一个环节。其中分页查询是提高用户体验和减轻服务器压力的一种常见手段。...
在实际使用中,继承自 HibernateDaoSupport 的 DAO 类可以通过重写其提供的方法,利用 HibernateTemplate 提供的便利功能,实现对业务对象的 CRUD 操作。例如,下面是一些使用 HibernateDaoSupport 的示例方法: ``...
【HibernateDaoSupport】是Spring框架中的一个抽象类,主要用于简化Hibernate的数据访问操作,它为DAO层提供了方便的事务管理和Session管理。这个类是Spring与Hibernate集成的重要组件,尤其对于初学者来说,理解其...
综上所述,在开发DAO层时,通过引入抽象层并合理利用`HibernateDaoSupport`等工具类,不仅可以降低代码与框架之间的耦合度,提高代码的可维护性和灵活性,还能有效应对未来可能的技术变迁带来的挑战。这种设计思想...
### HibernateDaoSupport与JdbcDaoSupport详解 #### 一、概述 在软件开发过程中,特别是企业级应用开发中,数据库操作是一项重要的任务。为了简化这一过程并提高代码的可维护性和扩展性,Spring框架提供了多种支持...
**JPA(Java Persistence API)与Hibernate:** JPA是Java平台上的一个标准,它定义了如何在Java应用中管理关系数据库。它提供了一种面向对象的方式来操作数据库,通过ORM(对象关系映射)技术将Java对象与数据库表...
本文将深入探讨如何利用Spring与Hibernate整合,通过两种不同的方式来实现这些基本操作:HibernateTemplate和HibernateDaoSupport。 **一、HibernateTemplate** HibernateTemplate是Spring提供的一个方便的类,它...
### HibernateDaoSupport与HibernateTemplate详解 #### 一、引言 在Java开发中,Spring框架与Hibernate框架的结合使用非常普遍。Spring框架以其强大的依赖注入(DI)和面向切面编程(AOP)功能,极大地简化了Java...
6. **DAO层支持**: 在描述中提到,我们可以选择让DAO层类继承`HibernateDaoSupport`。这个类是Spring ORM模块提供的,它提供了一个便捷的`getHibernateTemplate()`方法,可以直接使用HibernateTemplate进行数据操作...
在IT行业中,数据库管理和持久化框架是至关重要的组成部分,尤其是对于企业级应用开发。本文将深入探讨如何在MyEclipse 2014环境中利用Hibernate 3这一强大的ORM(对象关系映射)框架,生成数据库实体类和XML映射...