Create a project with MyEclipse JPA (3)

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

MyEclipse latest version download

This tutorial introduces some of the PA-based features in MyEclipse. While reading this tutorial, it will be helpful to understand the basic concepts of how JPA and entity mapping work with annotations. In this tutorial, you will learn how to:

  • Set up a project for JPA
  • connect to database
  • Reverse engineer a database table to generate entities
  • Implement create, retrieve, edit and delete functions

Duration: 30 minutes

Don't have MyEclipse? Download now

Fourth, write applications

Since MyEclipse generates a lot of code, you can quickly focus on writing the "business logic", or more specifically, "the code that actually executes". In this section, you will write a Java class with a main method that inserts the Productline into the database, retrieves and updates it, and then deletes it. Using this code, you should be able to see how easy it is to use JPA entities in your own application without writing JDBC code or any other persistence code.

4.1 Create a class

1. Right-click the com.myeclipseide.jpa package and select New>Class.

2. Enter RunJPA in the Name field , select the Public static void main check box, and click Finish.

generated class

After generating the new class and main method, you need to write code to successfully handle instances of Productline.

Note: The code below looks long and complicated, but that's because we're trying to show four different examples in one block of code. If you look at each operation (save, load, update, delete) they don't contain multiple lines of code.

3. Add the following code to the main method and press Ctrl + S to save.

/* 1. Create a reference to our ID */ 
             String productLineID = "Men's Shoes";

/* 2. Create a new Productline instance */ 
             Productline newProductline = new Productline(
                     productLineID,
                     "Shoes for men.",  "Men's Shoes", null); 

/* 3. Create a DAO instance to use */ 
             ProductlineDAO dao = new ProductlineDAO();

/* 4. Store our new product line in the DB  */ 
EntityManagerHelper.beginTransaction();
             dao.save(newProductline);
EntityManagerHelper.commit();

/* 5. Now retrieve the new product line,                   using the ID we created */ 
             Productline loadedProductline = dao.findById(productLineID);

/* 6. Print out the product line information  */ 
             System.out.println("*NEW* Product Line               [productLine="
                     +               loadedProductline.getProductline() + ",               textDescription="
                     +               loadedProductline.getTextdescription() + ", image="
                     +               loadedProductline.getImage() + "]");

/*        * 7. Now let's change  same value on the product line, and save the
     * change
 */ 
loadedProductline.setTextdescription("Product line for men's shoes.");
EntityManagerHelper.beginTransaction();
             dao.save(loadedProductline);
EntityManagerHelper.commit();

/*
   * 8. Now let's load the product line from the DB again, and make sure
   * it text description changed
  */ 
             Productline secondLoadedProductline =               dao.findById(productLineID);                System.out.println("*REVISED* Product Line ["
                 + "productLine=" +               secondLoadedProductline.getProductline()
                 + ", textDescription=" +               secondLoadedProductline.getTextdescription()
                 + ", image=" +               secondLoadedProductline.getImage() + "]");

/* 9. Now let's delete the product line from the DB */ 
EntityManagerHelper.beginTransaction();
             dao.delete(secondLoadedProductline);
EntityManagerHelper.commit();

/*       * 10. To confirm the deletion, try and load it again and make sure it
    * fails
 */ 
             Productline deletedProductline = dao.findById(productLineID);

/*
     * We use a simple inlined IF clause to test for null and print
     * SUCCESSFUL/FAILED
 */ 
             System.out.println("Productline deletion: "
                     + (deletedProductline ==               null ? "SUCCESSFUL" : "FAILED"));

Note:  It's a good idea to swap out code snippets for transactional databases with transactions, so if the operation fails (e.g. DB crashes) then all changes trying to happen in the transaction are rolled back to their original values ​​instead of only half the work done .

The code above looks daunting, but it does a lot of simple things back-to-back. For example, if you just want to store new items in the database, you only need the code for steps 1-3 in the program, which subtract three lines of code (minus comments). Here is a breakdown of each numbered section:

  1. The PRODUCTLINE table uses the name of the product line as the primary key. To make this tutorial easier to follow, we define the product line name in a string and reuse it throughout the code (create and store the product line, then retrieve it twice). You can easily type "Men's Shoes" multiple times, which we think makes the tutorial easier to follow.
  2. This will create a new instance of the Productline POJO generated by MyEclipse and will be inserted into the database. For the purposes of this tutorial, these values ​​are not critical, so we'll just use example information.
  3. This will create a DAO instance to use. Database access requires DAO, which is also generated by MyEclipse.
  4. This tells the DAO to store the new Productline in the database. Since you're going to be writing something to the database, wrap the save call in a transaction.
  5. To ensure that the Productline is stored correctly, using the ID defined in step 1, we ask the DAO to fetch the Productline and assign the result to a brand new object to ensure the loaded content is from the database. (We could assign this value to newProductline, but for the purposes of this tutorial we want to be pretty obvious where the object came from and before the instance loaded accidentally in the code doesn't exist).
  6. This will print out the value of the loaded entity to make sure it's the value that was just stored in the database.
  7. This changes the value on the POJO that was just loaded to show how updating records works. Then use DAO to commit the changes back to the database. Again, this operation is encapsulated in a transaction to ensure safe database changes.
  8. Just like in step 5, reload the record from the database using the ID defined in step 1 to ensure the update operation is valid. POJO values ​​will be printed to ensure the new description is saved to the database.
  9. This shows how to delete records from DB. Because this requires changes to the database, this code is encapsulated in a transaction.
  10. Similar to steps 8 and 5, to prove that the delete works, we try to load the entity from the database using the ID we provided. This operation should fail because we have removed the Productline. Once the result is obtained from the DAO, the print statement is printed with an embedded IF clause to ensure the result is empty.

For more information, please visit MyEclipse Chinese website>>

useful (1) 

Guess you like

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