The BEA Workshop ORM Workbench provides a powerful and flexible object relational
mapping interface for popular persistence services like BEA Kodo JPA. Depending
on the development scenario, OR Mappings can be generated through different
mechanisms:
Reverse Engineering the Schema to create object relational mappings
Generating Mappings from an Object Model
Generates annotated POJOs from schema using JPA ORM Generation wizard
In this step, we will use Workshop Studio to automatically generate JPA entity
beans from an existing database schema by reverse engineering the schema. Workshop
Studio will generate Java classes with the appropriate accessors and JPA annotations.
In the DbXplorer view, right-click the schema SalesDBConnection
and select Generate JPA Mapping.
Select JPA based Web application project, workshop-jpa-tutorial,
from the Web Application dialog. Workshop considers a web
application to be a JPA application if it has the Persistence file persistence.xml
under the META-INF folder.
Click OK.
Select the database tables from the JPA ORM Generation
dialog as shown below. Note that we are not selecting the CONTACT database
table at present because we will create a POJO class Contact
and annotate this class for JPA and that way we will understand the Top-Down
development approach.
You can also select the checkbox Copy driver and add to the class
path in case its enabled. It instructs the JPA ORM Generation
Wizard to add the JDBC driver for the Hypersonic Database to the
project classpath. Currently the driver already exists in the project buildpath.
Click Next
The Table Associations dialog displays entity relationships
as observed in the database schema from the foreign key definitions (shown
in green). The dialog allows you to edit a table association by selecting
each association and modifying its options in the editing panel.
Select an association between LINEITEM and ORDER_DATA. Change the property
from orderData to order.
Select an association between LINEITEM and PRODUCT. We want to define many-to-one
unidirectional relation between LINEITEM and PRODUCT. Hence, uncheck the checkbox
for generating a reference to a set of LineItems in Product.
Select an association between ORDER_DATA and CUSTOMER. Edit the property
from orderDatas to orders.
Click Next in the JPA ORM Generation
dialog.
In the dialog for Table Mapping under Key Generator,
select none strategy for key generation.
A Java Package is required for generating the entity beans. Hence, click
the Browse button for Java Package and
create a new Java package com.bea.beans using the Choose
Java Package dialog.
Click Next
The Tables and Columns dialog defines how the database
tables and columns will translate into generated entity beans and a generated
entity bean properties. Notice that the tooltip displays the table and column
properties as defined in the database schema.
Customize the tables and columns mapping generation as described below:
For the LINEITEM table, select the checkbox Implement equals/hashCode.
For the ORDER_DATA table, select the checkbox Implement equals/hashCode.
Click Next.
In the JPA ORM Generation dialog,ensure that the Update
JPA configuration file checkbox is selected, as it is by default.
This will ensure that the persistence.xml file is updated to include the
mapping information you've just defined.
Click Finish.
BEA Workshop Studio will generate all of the annotated Entity Java Beans
based on the properties supplied to the ORM Generation wizard.
Once generation is complete, the Entities can be accessed through the
AppXplorer view under the JPA Persistence Configuration
branch of the project. In the AppXplorer view, go
to the JPA Persistence Configuration branch under the workshop-jpa-tutorial
project. You can find the Object model under the package com.bea.beans
of the branch web/WEB-INF/src/java.
You can observe the set of Persistence entities generated by Workshop.
Workshop identifies the persistent entities based class names of entities
specified in persistence.xml file.
Workshop remembers the settings specified in the EJB ORM Generation
wizard to assist the next time you use it for the same database connection.
Review generated classes with annotations
When we create entity mappings, we define each property as having one of
six property types: basic, id, many-to-one, one-to-one, one-to-many, and many-to-many.
When you generate entities from a database, Workshop Studio annotates the
generated source code with JPA annotations that designate which type a given
property is. In this section we will review some basic JPA annotations.
Review the following annotations in the class Customer.
Basic Properties - A basic property handles a standard
value that is persisted as-is to the database.
A basic annotation is followed by the @Column annotation defining
an attribute name which is the name of the column to which the property
is bound; the attribute nullable is false to specify that the column
cannot store null values; the column length attribute specifies the
maximum length.
@Basic()
@Column(name="NAME", nullable=false, length=255)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
One-to-Many Properties - A one-to-many property designates
a relationship in which one A entity references multiple B entities,
and no two A entities reference the same B entity.
Here a Customer entity references multiple OrderData entities, but
an OrderData entity can have only one Customer entity reference.
The one to many annotation defines an attribute mappedBy which is
the name of the many-to-one field in OrderData entity that maps this
bidirectional relation.
@OneToMany(mappedBy="customer",
fetch=FetchType.EAGER)
public java.util.Set<OrderData> getOrders() {
return this.orders;
}
public void setOrders(java.util.Set<OrderData> orders) {
this.orders = orders;
}
Id properties - An Id property designates an identifier,
such as a primary key. All entity beans must declare one or more fields
which together form the persistent identity of an instance.
An Id annotation is followed by a @Column annotation defining the
attribute unique which is true to specify that the column is UNIQUE
in the SQL sense (can have only unique values).
Id()
@Column(name="CUSTOMERID", unique=true, nullable=false)
public Integer getCustomerid() {
return this.customerid;
}
public void setCustomerid(Integer customerid) {
this.customerid = customerid;
}
Review the following annotations in the class OrderData.
Many-to-one Properties - A many-to-one property designates
a relationship in which an entity A references a single entity B, and
other A's might also reference the same B; there is a many-to-one relation
from A to B.
The many to one annotation defines an attribute fetch which is a enum
specifying whether to load the field's persisted data before the entity
object is returned by the persistence provider (FetchType.EAGER) or
later, when the property is accessed (FetchType.LAZY).
The many to one annotation is followed by an @JoinColumn annotation
defining the column name which is the name of the column to which the
property is bound and a referencedColumnName attribute which is the
name of the primary key column being joined to.
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="CUSTOMERID", referencedColumnName="CUSTOMERID")
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
Run simple EJBQL to ensure that the mappings were generated properly and
the runtime is working
In this step, we will take a tour of the EJBQL Editor. The EJBQL Editor lets
you define and execute an EJBQL query.
In the DbXaminer view, go to the EJBQL Editor
tab and click the button to choose the workshop-jpa-tutorial
web application.
When defining an EJBQL query in the EJBQL Editor, Workshop
Studio provides code completion for EJBQL keywords and the persistent entities
by pressing CTRL+SPACE. Here, we will define an EJBQL SELECT
query using the code completion facility. In the EJBQL Editor, enter S
and press CTRL+SPACE. It displays a list of EJBQL keywords
starting with letter S.
Select the keyword select.
Enter a space and specify c as an alias to get the data
for the properties of persistent object Customer.
Enter from (You can use code completion for the from
keyword) and space.
Press CTRL+SPACE to get a list of persistent entities
and select the Customer object.
Enter space and specify an alias c for Customer entity.
Now, we have defined the EJBQL SELECT query. You can execute them by either
clicking the Execute Query button or by using the hotkey,
CTRL+ENTER.
Similarly execute an EJBQL SELECT query on persistent object OrderData
for a customer having a particular customerid.
Click one of the following arrows to navigate through the tutorial: