Spring整合Hibernate

一、Spring整合Hibernate有什么好处?

  1、由IOC容器来管理Hibernate的SessionFactory

  2、让Hibernate使用上Spring的声明式事务

二、事务配置

applicationContext.xml

<!-- Hibernate SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="namingStrategy">
            <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
                <prop key="secondLevelCacheManagerInstanceKey">${secondLevelCacheManagerInstanceKey}</prop>
                <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
                <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
            </props>
        </property>
        <property name="packagesToScan" value="cn.com.bidchina.*.*.entity,cn.com.bidchina.*.entity"/>
    </bean>
    

    <!-- Hibernate4 事务管理器配置,单数据源事务-->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
<!--定义事务传播属性-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*"    propagation="REQUIRED" read-only="true" />
            <tx:method name="list*"   propagation="REQUIRED" read-only="true" />
            <tx:method name="search*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*"   propagation="REQUIRED" read-only="true" />
            <tx:method name="load*"   propagation="REQUIRED" read-only="true" />
            <tx:method name="save*"   propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="add*"    propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="invoke*" propagation="REQUIRED" />
            <tx:method name="startWorkflow*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <!--定义切面 配置参与事务的类 -->
    <aop:config proxy-target-class="true">
            <aop:advisor
            pointcut="execution(* cn.com.bidchina..service.impl.*ServiceImpl.*(..))"
            advice-ref="txAdvice" />
            <aop:advisor
            pointcut="execution(* cn.com.bidchina.acl..service.impl.*ServiceImpl.*(..))"
            advice-ref="txAdvice" />
            <aop:advisor
            pointcut="execution(* cn.com.bidchina.dict..service.impl.*ServiceImpl.*(..))"
            advice-ref="txAdvice" />
    </aop:config>

需要注意的地方:

(1) advice(建议)的命名:由于每个模块都会有自己的Advice,所以在命名上需要作出规范,初步的构想就是模块名+Advice(只是一种命名规范)。

(2) tx:attribute标签所配置的是作为事务的方法的命名类型

         如<tx:method name="save*" propagation="REQUIRED"/>

        其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。

        propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择

(3) aop:pointcut标签配置参与事务的类,由于是在Service中进行数据库业务操作,配的应该是包含那些作为事务的方法的Service类。

       首先应该特别注意的是id的命名,同样由于每个模块都有自己事务切面,所以我觉得初步的命名规则因为 all+模块名+ServiceMethod。而且每个模块之间不同之处还在于以下一句:

       expression="execution(* com.test.testAda.test.model.service.*.*(..))"

       其中第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。

(4) aop:advisor标签就是把上面我们所配置的事务管理两部分属性整合起来作为整个事务管理。

@Transactional注解:

@Transactional 可以作用于接口、接口方法、类以及类方法上。当作用于类上时,该类的所有 public 方法将都具有该类型的事务属性,同时,我们也可以在方法级别使用该标注来覆盖类级别的定义。

虽然 @Transactional 注解可以作用于接口、接口方法、类以及类方法上,但是 Spring 建议不要在接口或者接口方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。另外, @Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。

原文地址:https://www.cnblogs.com/amunamuna/p/6509249.html