Create a project with MyEclipse JPA (2)

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

2. Create a Java package

In the next section, you will reverse engineer the data in the database tables and generate entities for your project. Before reverse engineering, you need to create a Java package that will place these entities.

1. Expand the project, right-click the src folder, and select New>Package.

Create a new package

2. Enter com.myeclipseide.jpa in the Name field and click Finish.

Name the Java package

3. Reverse design database table

Now that the project is set up, you are ready to reverse engineer the PRODUCTLINE table and convert it into a project and start using the generated entities.

1. Right-click the project and select MyEclipse>Generate Entities & DAOs.

Note: You can choose to use the MyEclipse reverse engineering tool or the DALI entity generator. Make your selections, click OK, and complete the wizard. This tutorial uses the MyEclipse reverse engineering tool.

Reverse Engineering Tool Selection

2. Select the PRODUCTLINE table, then click Add, click Next.

Reverse Engineering

3. In the Java Package field, click Browse and select the com.myeclipseide.jpa package you created earlier. Select the following checkboxes:

  • Entity Bean Generation : Tells MyEclipse to generate plain Java classes annotated as JPA entities.
  • Create abstract class : If you want to customize the generated class without worrying about overwriting the changes every time, MyEclipse can generate basic abstract classes as well as concrete subclasses that can be customized and used. Every time you reverse engineer, MyEclipse will only overwrite the abstract base class and maintain the changes in the concrete subclasses.
  • Update persistence.xml : Similar to Hibernate; you can list all JPA entities used in the JPA configuration file.
  • Java Data Access Generation : Tells MyEclipse to generate DAO utility classes for you that can instantly save/find/update/delete entities in the database. This code wraps the JPA entity manager to make working with entities and databases very easy.
  • Generate Precise findBy Methods : Tells MyEclipse to generate findByXXX methods, where XXX is associated with each property on the reversed entity. This makes it easy to access entities in the database using any attribute as a means of finding them.
  • Generate Java Interfaces : Selecting this option will create an interface class with the corresponding DAO implementation class. Deselecting this option will generate only the DAO implementation classes instead of the classes defining the interface separately.

Set up reverse engineering

4. Click Finish to view the MyEclipse resources generated by extending the com.myeclipseide.jpa package.

generated class

The generated entity description is as follows:

  • EntityManagerHelper : When using straight JPA, developers need to use the EntityManager class. This generated helper class makes working with EntityManager a little easier by providing static methods to access the manager and the most common operations that can be invoked at any time.
  • IProductline : The class that defines the corresponding DAO implementation class interface.
  • Productline : This class is a JPA entity (POJO) representing the database table PRODUCTLINE. This POJO contains the fields of the PRODUCTLINE table and represents a row in the DB.
  • ProductlineDAO : This class wraps the EntityManagerHelper and provides us with easy-to-use methods dedicated to adding/finding/updating and removing products from the database.

Note : After the reverse engineering is complete, the Persistence perspective can be opened to analyze the data in the database and project using some persistence and data source tools.

For more information, please visit MyEclipse Chinese website>>

Guess you like

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