Sun Java System Application Server |
This sample demonstrates the use of Java Persistence APIs based on the new Java Persistence specification within a servlet.
If you are familiar with EJB 2.1 persistence, you will notice many changes in this example based on the new Java Persistence specification:
There are no complex and arcane deployment descriptors. All metadata information is specified by annotations.
You can rely a great deal on defaults, so you code for the exceptional cases. This significantly reduces turnaround time for implementations.
Most importantly, you can switch your persistence implementation to any vendor that supports the Java Persistence specification.
The application is built and deployed as a WAR file.
The web module consists of:
A POJO which represents a Person. The persistence.xml file defines a persistence unit. The persistence unit uses jdbc/__default as the data source, and the java2db feature of glassfish to create tables.
A servlet that corresponds to listing current Person records in the database, and another that corresponds to creating a Person record in the database. Also, two JSP pages render the view.
The starting point of the application is defined as index.jsp which forwards to the ListPerson servlet. The servlet queries the list of Person records from the database using Java Persistence API. The list is passed on to ListPerson.jsp to render. You can click the Create a Person Record link at bottom of the listing to go to CreatePerson.jsp. This renders a form where you can enter data for a new Person record. When you click the CreateRecord button, the form is submitted to CreatePerson Servlet which creates a new Person record in the database using Java Persistence API.
Please note the pattern used to obtain an EntityManager instance in the servlets. It is always obtained from injected EntityManagerFactory. Here is a discussion about why this pattern is used instead of directly injecting an EntityManager.
The web-module is archived in web-jp-war.war.
Follow these steps to build, deploy, and run the application.
Set up your build environment and Configure the application server with which the build system has to work by following the common build instructions.
Make sure the JavaDB database is started. If not, refer to common build instructions.
Use the target all
to build, deploy and run the project.
Use the target clean
to remove the temporary
directories like build and dist
Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.
samples_install_dir/javaee5/enterprise/web-jpa-war
as the project.web-jpa-war
and select Run Project
which will build, deploy and run the project. If configured properly, the IDE will open the browser with appropriate url. Sample output is given below. All operations completed successfully run-deploy: Browsing: http://localhost:8080/web-jpa-war/ run-display-browser: run: BUILD SUCCESSFUL (total time: 2 seconds)Browser output
http://localhost:8080/web-jpa-war/ List of Persons currently in Database ID FirstName LastName Create a Person Record
If you have problems when running the application, refer to troubleshooting document.
Copyright © 2006 Sun Microsystems, Inc. All rights reserved.