Spring框架(JavaEE开发进阶Ⅲ)—在Spring中集成Hibernate

一、主要内容

1、Hibernate概览
2、声明Hibernate的Session工厂
3、构建不依赖于Spring的Hibernate代码

二、前言

1、应用程序越复杂,对持久化需求随之要求更高
1)延迟加载(lazy loading)
2)预先抓取(Eager fetching)
3)级联(Cascading)

2、ORM(object-relational mapping)工具应运而生

3、Spring对ORM框架的支持
1)Spring声明式事务的集成支持
2)透明的异常处理
3)线程安全的、轻量级的模板类
4)DAO支持类
5)资源管理

三、Hibernate概览

1、以前,Spring应用程序使用Hibernate是通过HibernateTemplate进行的,HibernateTemplate的职责之一是管理Hibernate的Session。HibernateTemplate存在一定程序的侵入性,多少与Spring API产生耦合
2、Hibernate3引入了上下文Session(Contextual session)是Hibernate本身提供的保证每个事务使用同一Session的方案。该方式使DAO类不包含特定Spring代码

四、声明Hibernate的Session工厂

1、Hibernate的主要接口org.hibernate.Session提供基本的数据访问功能(保存、更新、删除和从数据库加载对象等)
2、获取Hibernate Session对象的标准方式是借助Hibernate的SessionFactory接口实现类,SessionFactory主要负责Hibernate Session的打开、关闭及管理
3、在Spring中,通过Spring的某个Hibernate Session工厂Bean来获取Hibernate的SessionFactory。可以在应用的上下文中配置Hibernate Session工厂
4、如果持久化对象通过XML文件配置——即在XML中定义对象与数据库之间的映射,则在Spring中配置LocalSessionFactoryBean:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
            <value>User.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.OracleDialect</prop>
        </props>
    </property>
</bean>
5、如果使用注解的方式来定义持久化,则在Spring中使用AnnotationSessionFactoryBean代替LocalSessionFactoryBean:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packageToScan" value="com.iotek.myTwitter.domain"/>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.OracleDialect</prop>
        </props>
    </property>
</bean>
6、使用packageToScan属性告诉Spring扫描若干包以查找域类,这些类通过注解方式表明要使用Hibernate进行持久化
7、如果使用Hibernate4,注解方式和XML文件配置方式同一配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packageToScan" value="com.iotek.myTwitter.domain"/>
    <property name="hibernateProperties">
        <props>
            <prop key="dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
        </props>
    </property>
</bean>
8、对于通过注解方式表明要使用Hibernate进行持久化的类,需要注解标注

五、构建不依赖于Spring的Hibernate代码

1、Hibernate能够自己管理Session,不需要Spring的Hibernate模板,可以直接将Hibernate Session装配到DAO类中

2、示例代码说明:
1)通过@Autowired注解可以让Spring自动将一个SessionFactory注入到HibernateDao的SessionFactory属性中,该属性用来获取当前事务的Session
2)@Respository注解能被Spring的<context:component-scan>扫描到
<context:component-scan base-package="com.iotek.myTwitter.persistence"/>
3)为了给不使用模板的Hibernate DAO添加异常转换功能,在Spring应用上下文中添加一个PersistenceExceptionTranslationPostProcessorBean
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
该Bean会在所有用用@Repository注解的类上添加一个通知器(advisor),会捕获任何平台相关的异常并以Spring的非受查数据访问异常形式重新抛出
3、配置文件中声明事务(需要引入AOP和tx命名空间)
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:advisor pointcut="execution(* *..UserService.*(..))" advice-ref="txAdvice"/>
</aop:config>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值