Spring框架(JavaEE開發進階Ⅲ)—在Spring中整合Hibernate
阿新 • • 發佈:2019-02-03
一、主要內容
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工廠
2、獲取Hibernate Session物件的標準方式是藉助Hibernate的SessionFactory介面實現類,SessionFactory主要負責Hibernate Session的開啟、關閉及管理
3、在Spring中,通過Spring的某個Hibernate Session工廠Bean來獲取Hibernate的SessionFactory。可以在應用的上下文中配置Hibernate Session工廠
4、如果持久化物件通過XML檔案配置——即在XML中定義物件與資料庫之間的對映,則在Spring中配置LocalSessionFactoryBean:
5、如果使用註解的方式來定義持久化,則在Spring中使用AnnotationSessionFactoryBean代替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>
6、使用packageToScan屬性告訴Spring掃描若干包以查詢域類,這些類通過註解方式表明要使用Hibernate進行持久化<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>
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>