Spring注入JPA+JPA交易管理,spring注入jpa事務

來源:互聯網
上載者:User

Spring注入JPA+JPA交易管理,spring注入jpa事務

 

  本例實現的是Spring注入JPA 和 使用JPA交易管理。JPA是sun公司開發的一項新的規範標準。在本質上來說,JPA可以看作是Hibernate的一個子集;然而從功能上來說,Hibernate是JPA的一種實現。

  在web開發的過程中,使用hibernate進行資料庫連接、事務等的管理。當然也可以使用JPA替換Hibernate是實現這些功能。

 

  一、使用Spring來注入JPA

    之前,在本人部落格(http://www.cnblogs.com/demoMeng/)中已經進行了hibernate的搭建和後期的是實現。瞭解到了,擷取hibernate中的sessionFactory有兩種方式。

    一種是自己手動擷取src目錄下hibernate.cfg.xml設定檔:(在dao層面的代碼)

        SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

 

    另外一種是使用spring注入,並且可以刪除src目錄下的設定檔:

      在spring的設定檔applicationContext.xml檔案中進行sessionFactory和dataSource的bean的配置:之後在dao層面的實作類別中定義一個屬性為SessionFactory,使用spring注入

 

            

          同理,使用JPA也有這兩中方式來是實現。先前,本人部落格中已進行了JPA第一種的實現(手動書寫代碼讀取JPA設定檔):http://www.cnblogs.com/demoMeng/p/5853583.html,所以在此,進行第二種使用spring注入的方式是實現擷取EntityManagerFactory。

 

 

      1、首先:在spring設定檔建立一個bean標籤,目的建立一個EntityManagerFactory,這裡不同於sessionFactory的bean,因為這裡讀取的是src目錄下的META-INF目錄下的persistence.xml 即JPA的設定檔,所以在下面設定的是持久化單元名"myJpa",對應JPA設定檔中的單元名字。

      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">                  <property name="persistenceUnitName" value="myJpa"/>            </bean>  

 

      JPA的設定檔 :persistence.xml,這裡的具體不做過多的解釋,如果有疑問,請上網尋找資料。

<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">    <persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL">        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>        <class>entity.Product</class>
<properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="123456"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/ProductDB"/> <property name="hibernate.show_sql" value="true"></property> <property name="hibernate.format_sql" value="true"></property> <property name="hibernate.connection.autocommit" value="false"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit></persistence>

 

        

       2、最後:在dao層面設定sessionFactory成員屬性。

                           

                      在這裡,可以使用spring的註解,或者是使用bean注入的方式,這裡使用的spring的注入進行注入。使用spring註解需要在spring設定檔中添加一個註解的解析器:,這個@Qualifier("entityManagerFactory")註解中的entityManagerFactory對應的是spring設定檔中JPA配置的entityManagerFactory的id屬性值。

 

 

      二、添加JPA的交易管理

         1、首先建立在之前使用spring注入JPA的基礎之上,再加入JPA的交易管理。好處 不需要手動開啟、提交事務和關閉資源,因為這些都是重複的代碼,做的事情都是同一樣的。所以可以交給事務來管理。

          首先:在spring設定檔applicationContext.xml中添加交易管理,如下 

            <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">                       <property name="entityManagerFactory" ref="entityManagerFactory"/>              </bean>              <tx:annotation-driven transaction-manager="txManager"/> 

 

          其次:在dao層面加上註解@Transactional。這個註解需要載入類上。並且在這個dao類上加上一個屬性:之後就可以直接使用這個entityManager進行CURD操作。

          這個屬性: 的定義是建立在JPA的交易管理之上的,當web容器讀取spring設定檔中的entityManagerFactory後會自動建立一個entityManager,然後根據在dao類中定義的這個屬性進行注入,和上面的定義EntityManagerFactory不同。具體的可以自己進行嘗試。  

 

  

 

 

      謝謝瀏覽! 

        

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.