10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

25<br />

CHAPTER 25<br />

Persistence in theWebTier<br />

This chapter describes how to use the <strong>Java</strong> Persistence API from web applications. <strong>The</strong> material<br />

here focuses on the source code and settings of an example called bookstore, a web application<br />

that manages entities related to a book store. This chapter assumes that you are familiar with the<br />

concepts detailed in Chapter 24, “Introduction to the <strong>Java</strong> Persistence API.”<br />

Accessing Databases fromWeb Applications<br />

Data that is shared between web components and is persistent between invocations of a web<br />

application is usually maintained in a database. Web applications use the <strong>Java</strong> Persistence API<br />

(see Chapter 24, “Introduction to the <strong>Java</strong> Persistence API”) to access relational databases.<br />

<strong>The</strong> <strong>Java</strong> Persistence API provides a facility for managing the object/relational mapping (ORM)<br />

of <strong>Java</strong> objects to persistent data (stored in a database). A <strong>Java</strong> object that maps to a database<br />

table is called an entity class. It is a regular <strong>Java</strong> object (also known as a POJO, or plain, old <strong>Java</strong><br />

object) with properties that map to columns in the database table. <strong>The</strong> Duke’s Bookstore<br />

application has one entity class, called Book that maps to WEB_BOOKSTORE_BOOKS.<br />

To manage the interaction of entities with the <strong>Java</strong> Persistence facility, an application uses the<br />

EntityManager interface. This interface provides methods that perform common database<br />

functions, such as querying and updating the database. <strong>The</strong> BookDBAO class of the Duke’s<br />

Bookstore application uses the entity manager to query the database for the book data and to<br />

update the inventory of books that are sold.<br />

<strong>The</strong> set of entities that can be managed by an entity manager are defined in a persistence unit. It<br />

oversees all persistence operations in the application. <strong>The</strong> persistence unit is configured by a<br />

descriptor file called persistence.xml. This file also defines the data source, what type of<br />

transactions the application uses, along with other information. For the Duke’s Bookstore<br />

application, the persistence.xml file and the Book class are packaged into a separate JAR file<br />

and added to the application’s WAR file.<br />

701

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!