Generating JPA Entity Classes

Generating JPA Entity Classes

The Java Persistence API (JPA) specification is focused on simplifying and standardizing data access. This is done by means of facilities such as Java annotations and a coherent XML file known as a “persistence unit,” by defining the connection between the application and the databases it needs to access. The IDE further simplifies the JPA specification by letting you generate your JPA entity classes from tables in your database, as you will discover in this section.

In the New File window, which you can access via File ➤ New File or Ctrl+N, choose Persistence ➤ Entity Classes from Database, as shown in Figure 5-7 .

Figure 5-7. Generating JPA entity classes from a database Also notice that the Persistence category in the New File window provides a number of other file

generators. For example, if you want to expose your data as RESTful Web Services, choose RESTful Web Services from Database in the window shown in Figure 5-7 , because this will generate your JPA entity classes together with your RESTful Web Services, all at the same time in a single process. Simultaneously, a persistent unit will be created. Notice that a persistence unit can also be created separately, by means of the Persistence Unit template, also shown in Figure 5-7 .

When you click Next and select a data source, which in the case of this scenario is jdbc/sample, the available tables in the data source are listed in the Available Tables list, as shown in Figure 5-8 .

Figure 5-8. Available tables in selected data source Notice that the list of tables matches the list of tables shown in Figure 5-1 . In other words, the tables that

you had access to when setting up the database connection in the IDE are available in the Database Tables step of the New Entity Classes from Database wizard.

To choose tables for use in your application, select them in the Available Tables list and click Add. This will move them to the Selected Tables list on the right side of the Database Tables window, as shown in Figures 5-8 and 5-9 .

Figure 5-9. Tables with foreign key relationships automatically included

For purposes of this scenario, select CUSTOMER and click Add. This will move the CUSTOMER table to the Selected Tables list of the Database Tables window. As you can see in Figure 5-9 , tables with foreign key relationships are selected automatically. When you click Next, you move to the Entity Classes step, as shown in Figure 5-10 . The New Entity

Classes step displays the Class Names list at the top, showing you the tables you selected, as well as the class names that will be used when the IDE automatically generates a JPA entity class to access the data from the related table.

Figure 5-10. Entity Classes step in New Entity Classes from Database wizard Also, if the class already exists, the Generation column, shown in Figure 5-10 , displays “Update”, instead

of “New,” to inform you that the IDE will override the existing JPA entity class with a new JPA entity class at the end of the New Entity Classes from Database wizard.

Notice that several checkboxes are selected by default, so the IDE will automatically generate named queries for persistent fields, JAXB annotations, and a persistence unit.

Although you can click Finish here, as shown in Figure 5-10 , you can also click Next to fine-tune the settings so that the generated code will match your requirements exactly, as shown in Figure 5-11 .

Figure 5-11. Fine-tuning the settings to define the code to be generated

When you click Finish, the JPA entity classes are generated, based on the settings you provided in the preceding steps of the wizard. By default, the persistence unit is also created, as shown in Figure 5-12 .

Figure 5-12. Generated JPA entity classes, with related dependencies and persistence unit All the dependencies that the Maven-based application needs to access are registered in the POM and

visualized in the Dependencies node, as shown in Figure 5-12 .

Now that you have your JPA entity classes, you can use of a variety of tools in the IDE to access your database and do something with the related data.