Sun Java System Application Server |
This sample demonstrates the use of Java Persistence APIs based on the new Java Persistence specification. It interacts with a relational database to store and display information about customer subscriptions to periodicals.
If you are familiar with EJB 2.1 persistence, you will notice a lot of changes in this example based on the new Java Persistence specification. Although this application provides the same functionality as a corresponding EJB 2.1 implementation, it is much simpler:
The application is built and deployed as an EAR file. It consists of two modules: the web module and the EJB module.
The web module consists of JSP files that contain the presentation
logic for the sample.
The starting point of the application is the mainindex.html
page,
from which you can navigate to different
pages to perform tasks such as creating a customer, searching for a
customer, searching for an address,
creating more subscriptions, and editing a customer profile. Each of
these pages has
a [HOME] link, that returns you to the main page.
The EJB module consists of a stateless session bean, the persistence unit and some supporting classes. The JSP files look up the CustomerSession stateless session bean and, based on the client inputs, invoke its business methods.
The CustomerSession stateless session bean uses resource injection to get access to an EntityManager. It uses the entity manager for all persistence related operations, such as inserts, updates and lookups. The entity classes are Address, Customer and Subscription. These are plain old Java objects (POJOs) with metadata annotations that describe their database mappings, associations with other entities, and fetch strategy.
The persistence classes are archived in customer-cmp-ejb.jar
along with a
persistence.xml
, that defines
the EntityManager provider class and the database connection
information for the persistence unit.
The web-module and ejb-module are archived in customer-cmp-ear.ear
.
Follow these steps to build, deploy, and run the application.
app_dir
is the sample application base
directory: install_dir/samples/ws/javaee5/enterprise/customer-cmp-ear
app_dir.
Make sure the JavaDB database is started. If not, refer to common build instructions.
Change directory to app_dir/setup
app_dir/setup> ant create-db
Change directory to app_dir
app_dir> ant all
app_dir> ant setup
creates jdbc-resources app_dir> ant default
compiles and packages
the application app_dir> ant deploy
deploys it to
application server app_dir> ant run
runs the sample from a
browser using the URL http://[hostname]:8080/customer-cmp-war/index.html
app_dir> ant unsetup
app_dir> ant clean
Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.
samples_install_dir/javaee5/enterprise/customer-cmp-ear
as the project.customer-cmp-ear
and select Run Project
which will build, deploy and run the project. If configured properly, the IDE will open the browser with the url http://localhost:8080/customer-cmp-war
. Sample output is given below. post-init: init-check: init: run-ac: BUILD SUCCESSFUL (total time: 22 seconds)Browser output
http://localhost:8080/customer-cmp-war/ Simple CMP Example -- Customer Create New Customer Search For Customer Create New Subscription Search For Subscription Search For Address
If you have problems when running the application, refer to troubleshooting document.
Copyright © 2006 Sun Microsystems, Inc. All rights reserved.