Sun Java System Application Server 

Samples Index

The Java Persistence Sample Application

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:

Application Structure

The application is built and deployed as an EAR file. It consists of two modules: the web module and the EJB module.

Web 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.

EJB Module

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.

Application archive

The web-module and ejb-module are archived in customer-cmp-ear.ear.

Building, Deploying, and Running the Application

Follow these steps to build, deploy, and run the application.

  1. Set up your build environment and Configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: install_dir/samples/ws/javaee5/enterprise/customer-cmp-ear
  3. Change directory to app_dir.
  4. Make sure the JavaDB database is started. If not, refer to common build instructions.

  5. Create database

    Change directory to app_dir/setup

    app_dir/setup> ant create-db

  6. Set up JDBC resources, Compile, Deploy and Run the sample application using the target all:

    Change directory to app_dir

    app_dir> ant all

  7. It is possible to do the above separately by issuing separate commands:
    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
  8. Remove the database resources:

    app_dir> ant unsetup

  9. Use the target clean to remove the temporary directories like build and dist

    app_dir> ant clean

Building, Deploying, and Running the Application in NetBeans IDE

Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.

  1. Refer to common build instructions. for setting up NetBeans IDE and the application server with which the IDE will use.
  2. In NetBeans IDE, select File->OpenProject and select samples_install_dir/javaee5/enterprise/customer-cmp-ear as the project.
  3. Right click on 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.
  4. NetBeans IDE output
      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 
      

Troubleshooting

If you have problems when running the application, refer to troubleshooting document.

 

Copyright © 2006 Sun Microsystems, Inc. All rights reserved.