MyEclipse持续性开发教程:用JPA和Spring管理数据(五)

MyEclipse 3.15 Style——在线购买低至75折!火爆开抢>>

MyEclipse最新版下载

本教程介绍了MyEclipse中的一些基于JPA / Spring的功能。有关设置JPA项目的基础知识,请先阅读JPA教程。 本教程主要关注MyEclipse中的JPA-Spring集成以及如何利用这些函数。您将学习到:

  • 为JPA和Spring建立一个项目
  • 反向设计一个数据库表来生成实体
  • 实现创建,检索,编辑和删除功能
  • 启用容器管理的事务

持续时间:30分钟

没有MyEclipse? 现在下载

四、启用Spring容器管理事务

除了用户管理事务外,Spring还通过@Transactional属性支持容器管理事务。 对于容器管理的事务支持,当您添加facets时,必须启用它,在前面的部分已经介绍过。

MyEclipse持续性开发教程:用JPA和Spring管理数据(五)
启用对@Transactional注释的支持

启用它会将以下事务元素添加到您的bean配置文件中。 您还应该添加一个JPAServiceBean,它用于删除使用容器管理的事务实体。 请参阅下面的实现:

MyEclipse持续性开发教程:用JPA和Spring管理数据(五)
注释驱动的配置元素

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中文网>>

上一篇:WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示


下一篇:MyEclipse教程:使用UML创建模块库——第一部分(一)