EE5 Travel Center Entities Project
Creating The Persistence Unit and Entity Classes
This package contains Java Persistence Architecture entity
classes, and a corresponding persistence unit definition, for the tables
found in the Travel Center example database that ships with Creator 2
Update 1, using JavaDB. It was built with NetbBeans 5.5 Beta 2, by
using the following steps:
- Using the command line or appropriate menu option, start the
copy of Derby that shipped with Creator 2 Update 1, which
listens on port 21527.
- Select the Runtime tab, right click on Databases,
and select the New Connection ... option.
- Fill out the following properties in the dialog and click OK:
- Name: Java DB (Net)
- Driver: (leave default)
- Database URL: jdbc:derby://localhost:21527/sample
- Username: travel
- Password: travel
- You should now see a new connection for this database under the
Databases node. Expand it, expand tables, right click
on the PERSON table, and select the View Data option.
You should see a new window open with rows from the PERSON table.
- From the main menu, select File --> New File ... then select
category Persistence, and file type Persistence Unit
- Fill out the following properties in the dialog and click FINISH:
- Persistence Unit Name: travel
- Persistence Library: TopLink
- Database Connection: jdbc:derby://localhost:21527/sample [travel on TRAVEL]
- Table Generation Strategy: create
- NetBeans will create a
persistence.xml
configuration file
for the new persistence unit, and open it.
- From the main menu, select File --> New File ... then select
category Persistence and file type Entity Classes
From Database.
- In the Projects tab, right click on the
EE5TravelEntities
node and select Clean and Build Project.
At this point, we have a set of entity classes that correspond to each of the
tables in the Travel Center database. It would be possible to customize things
along the way, or to add additional custom methods -- but those can be left for
later development.
This library of entity classes can now be used in a variety of different ways.
Additional projects will demonstrate using these classes:
- Accessed directly from page beans (similar to how Creator 2 uses
cached rowsets to mediate access to the database).
- Accessed indirectly from a plain old JavaBean containing
business logic, but which receives resource injection if it
is a JSF managed bean.
- Accessed indirectly from an EJB3 session bean.
- Accessed indirectly from a JAX-WS web service.
To achieve these objectives, a business logic bean Logic.java
was hand created in the ee5travel.logic
package. This allows
the development of a single copy of the actual modification logic, which can
then be reused in the above scenarios.
Defining a JDBC Data Source In Glassfish
As supplied, the persistence unit in this project specifies that
a JDBC data source named "jdbc/travel" be used to access the data.
Before you can deploy an application that uses these classes, then,
you must do a one-time configuration of this data source in the
Glassfish instance you will be deploying to. This configuration is
accomplished using the administration console for the server,
as follows:
- On the Runtime tab, ensure that the Glassfish
instance is running.
- Right click on the Glassfish instance, and select
View Admin Console.
- Log in to the admin console, and navigate to the
Resources --> JDBC --> Connection Pools page.
- Press the New button, fill out the following
settings on the first page, and press Next:
- Name: TravelCenterPool
- Resource Type: javax.sql.DataSource
- Database Vendor: Derby
- On the second page, scroll down to the Additional
Properties section, and customize the following values
(the values shown below assume you are using the Derby
instance from Creator 2 Update 1), then press Finish:
- PortNumber: 21527
- User: travel
- Password: travel
- ServerName: localhost
- DatabaseName: sample
- You will be returned to the Connection Pool list page.
Click on the new TravelCenterPool entry to see the
settings, and press the Ping button to ensure
that you can successfully connect to the database
(which must be up and running for this to work).
If the ping is not successful, edit any settings that
need to be changed, and press Save.
- Now, navigate to the Resources --> JDBC --> JDBC Resources
page.
- Press the New button, and fill out the
following values and press OK:
- JNDI Name: jdbc/travel
- Pool Name: TravelCenterPool
- Status: Leave the "enabled" box checked
- Your new JDBC data source is now configured and available
for any application that wants to use it.