Hibernate 4中的LocalSessionFactoryBean和hibernate 3中的AnnotationSessionFactoryBean之间的区别

时间:2017-02-02 00:02:22

标签: spring hibernate

在一个项目中,我们希望将hibernate版本从3.6升级到4.3。

在hibernate 3中,我们使用AnnotationSessionFactoryBean:

 <bean id="AbstractSessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        abstract="true">
    <!--  <property name="packagesToScan" value="com.amazon.layout.dao.model" /> -->
    <property name="annotatedClasses">
        <list>
            <value>com.amazon.layout.dao.model.EdgeModel</value>
            <value>com.amazon.layout.dao.model.VertexModel</value>
            <value>com.amazon.layout.dao.model.PhysicalResourceToVertexMappingModel</value>
        </list>
    </property>
    <property name="exposeTransactionAwareSessionFactory">
        <value>true</value>
    </property>
 </bean>

 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="SessionFactory" />
 </bean>

 <!-- Use Spring transactions for Hibernate -->
 <tx:annotation-driven transaction-manager="txManager" mode='aspectj' proxy-target-class='true' />
 </beans>

在hibernate 4.3中,我们使用LocalSessionFactoryBean:

 <bean id="AbstractSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        abstract="true">
    <!--  <property name="packagesToScan" value="com.amazon.layout.dao.model" /> -->
    <property name="annotatedClasses">
        <list>
            <value>com.amazon.layout.dao.model.EdgeModel</value>
            <value>com.amazon.layout.dao.model.VertexModel</value>
            <value>com.amazon.layout.dao.model.PhysicalResourceToVertexMappingModel</value>
        </list>
    </property>
 </bean>

 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory" ref="SessionFactory" />
 </bean>

 <!-- Use Spring transactions for Hibernate -->
 <tx:annotation-driven transaction-manager="txManager" mode='aspectj' proxy-target-class='true' />
</beans>
  1. 这两个是等价的吗?
  2. LocalSessionFactoryBean中没有exposeTransactionAwareSessionFactory属性。安全吗?

1 个答案:

答案 0 :(得分:0)

来自LocalSessionFactoryBean的javadoc:

  

此类的作用与orm.hibernate3包中的同名类相似。但是,在实践中,它更接近AnnotationSessionFactoryBean,因为它的核心目的是从包扫描中引导SessionFactory。

LocalSessionFactoryBean未公开该方法的原因是它不必要。