MyEclipse持續性開發教程:用JPA和Spring管理數據(五)

MyEclipse 3.15 Style——在線購買低至75折!火爆開搶>>數據庫

MyEclipse最新版下載eclipse

本教程介紹了MyEclipse中的一些基於JPA / Spring的功能。有關設置JPA項目的基礎知識,請先閱讀JPA教程。 本教程主要關注MyEclipse中的JPA-Spring集成以及如何利用這些函數。您將學習到:函數

  • 爲JPA和Spring創建一個項目
  • 反向設計一個數據庫表來生成實體
  • 實現建立,檢索,編輯和刪除功能
  • 啓用容器管理的事務

持續時間:30分鐘學習

沒有MyEclipse? 如今下載ui

4、啓用Spring容器管理事務

除了用戶管理事務外,Spring還經過@Transactional屬性支持容器管理事務。 對於容器管理的事務支持,當您添加facets時,必須啓用它,在前面的部分已經介紹過。this

啓用對@Transactional註釋的支持spa

啓用它會將如下事務元素添加到您的bean配置文件中。 您還應該添加一個JPAServiceBean,它用於刪除使用容器管理的事務實體。 請參閱下面的實現:設計

註釋驅動的配置元素blog

JPAServiceBean實現以下所示;請注意deleteProductLine方法上的@Transactional註釋以及缺乏任何用戶管理的事務語句。教程

public class JPAServiceBean  {
      private IProductlineDAO dao; @Transactional public void deleteProductLine(String productlineID)  		  { /* 1. Now retrieve the new product line, using the ID we created */Productline loadedProductline = dao.findById(productlineID);  			   /* 2. Now let's delete the product line from the DB */   dao.delete(loadedProductline);  			   /*   * 3. To confirm the deletion, try and load it again and make sure it * fails   */ 
      Productline deletedProductline = dao.findById(productlineID);  
/*    * 4. We use a simple inline IF clause to test for null and print * SUCCESSFUL/FAILED   */ 
      System.out.println("Productline deletion: " + (deletedProductline == null ? "SUCCESSFUL" : "FAILED"));}  			   public void setProductLineDAO(IProductlineDAO dao) { this.dao = dao; }
}

從應用程序上下文中獲取JPAServiceBean的一個實例並按以下所示使用它:

JPAServiceBean bean = (JPAServiceBean) ctx.getBean("JPAServiceBean");
 bean.deleteProductLine(productlineID);

更多資訊敬請訪問MyEclipse中文網>>

相關文章
相關標籤/搜索