Workshop greatly eases the process of creating a JPA persistence layer. It provides these basic paths to creating EJB3 entities:
Of course, Workshop also supports creating a persistence.xml and annotating class source code manually.
Before you get started, you'll need a web application project that supports JPA. Also, while you can do it along the way, it's a good idea to have set up a database connection in advance.
You can create EJB3 entity beans from an existing database schema. When you do, Workshop will generate Java classes with the appropriate accessors and EJB3 annotations. Your entity beans become the persistence layer interacting with that database.
For illustration purposes, this topic uses a connection to an employee database.
After you've selected a connection, select a schema (if one is available), then select the tables from which entity beans should be generated. Leave the Copy driver and add to the class path check box selected if you want the driver to be available to the code that uses your persistence layer. (Of course, if you clear the check box, you can add a driver library later using the project properties dialog.) Note that you are generate one entity bean for each table selected in the dialog.
With tables selected, click Next.
This dialog displays the table associations as observed in the database schema from foreign key relationships. Green boxes designate associations that will be translated into entity relationships. Use this dialog to determine the associations you want generated for your entity beans.
The following illustrates how you might add a categories-products relationship if it didn't already exist.
Note that all the properties are optional, but you should be sure to enter a value for the Java Package box; this will be the package into which your generated entity beans will go. If you haven't yet created a package for these classes, you can create one by clicking the box's Browse button.
The Key Generator dropdown lists values that determine how a primary key should be generated. The available values include:
|
||||||||||||||
The Entity Access dropdown lists values that determine how data in entity objects will be accessed. The available values include:
|
||||||||
When you select a table in the schema tree, the area beneath the tree will display how that table will translate into a generated entity bean. Under Table Mapping, leave the default values or edit characteristics for the generated entity bean. By default, the schema's table names are used for generated class names. Note that tooltips display the table and column properties as defined in the database schema.
When you select a column in the schema tree, the area beneath the tree will display how that column will translate into a generated entity bean property. Under Column Mapping, leave the default values or edit characteristics for the generated entity bean property.
You can now save the state of the ORM Generation wizard prior to ORM Entity bean generation. This allows you to use the mapping wizard to specify some of your O/R mappings, save the state, and return at a later time to add additional mappings before generating. On the last page of the wizard, click Save wizard state to save the current mappings without generating.
You can use a database schema to annotate existing Java classes (POJOs) to make them into EJB3 entity beans. When you do, Workshop will add EJB3 annotations to the appropriate accessors.
For illustration purposes, this topic uses a connection to an employee database.
|
||||||||
In other words, if you choose property, Workshop will annotate your class's accessors; if you choose field, it will annotate your class's fields.
The following shows that the employee table will be mapped to the sample.employeeinfo.Employee
class selected above. It also shows that that class's employeeId
field will be mapped to the table's primary key.
Under Bean Properties Mapping, verify or edit mappings between database columns and fields discovered in the Java class you'll be annotating. Note that you can click the Edit button to display the Basic Property Tag dialog for editing a selected property mapping.
Workshop will add EJB3 annotations to your Java class source code. These annotations will map the class and its properties or fields to the selected table and columns.
Exploring Databases with the DbXplorer
Managing EJB3 Entity Relationships