MyEclipse Continuous Development Tutorial: Managing Data with JPA and Spring (5)

MyEclipse 3.15 Style - Up to 25% Off Buy Online! Hot open grab>>

MyEclipse latest version download

This tutorial introduces some of the JPA/Spring based features in MyEclipse. For the basics of setting up a JPA project, read the JPA tutorial first . This tutorial focuses on JPA-Spring integration in MyEclipse and how to take advantage of these functions. You will learn:

  • Build a project for JPA and Spring
  • Reverse engineer a database table to generate entities
  • Implement create, retrieve, edit and delete functions
  • Enable container-managed transactions

Duration: 30 minutes

Don't have MyEclipse? Download now

4. Enable Spring Container Managed Transactions

In addition to user-managed transactions, Spring also supports container-managed transactions through the @Transactional attribute. For container-managed transaction support, it must be enabled when you add facets, as described in the previous section.

Enable support for @Transactional annotation

Enabling it will add the following transaction element to your bean configuration file. You should also add a JPAServiceBean which is used to delete transaction entities managed using the container. See the implementation below:

Annotation-driven configuration elements

The JPAServiceBean implementation is shown below; note the @Transactional annotation on the deleteProductLine method and the lack of any user-managed transaction statements.

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; }
}

Get an instance of JPAServiceBean from the application context and use it as follows:

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

For more information, please visit MyEclipse Chinese website>>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325338679&siteId=291194637