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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Accessing Databases fromWeb Applications<br />

702<br />

As in JDBC technology, a DataSource object has a set of properties that identify and describe<br />

the real world data source that it represents. <strong>The</strong>se properties include information such as the<br />

location of the database server, the name of the database, the network protocol to use to<br />

communicate with the server, and so on.<br />

An application that uses the <strong>Java</strong> Persistence API does not need to explicitly create a connection<br />

to the data source, as it would when using JDBC technology exclusively. Still, the DataSource<br />

object must be created in the Application Server.<br />

To maintain the catalog of books, the Duke’s Bookstore examples described in Chapters<br />

“Further Information about Web Applications” on page 98 through “Including the Classes,<br />

Pages, and Other Resources” on page 463 use the <strong>Java</strong> DB database included with the<br />

Application Server.<br />

To populate the database, follow the instructions in “Populating the Example Database” on<br />

page 97.<br />

To create a data source, follow the instructions in “Creating a Data Source in the Application<br />

Server” on page 98.<br />

This section describes the following:<br />

■ “Defining the Persistence Unit” on page 702<br />

■ “Creating an Entity Class” on page 703<br />

■ “Obtaining Access to an Entity Manager” on page 704<br />

■ “Accessing Data from the Database” on page 706<br />

■ “Updating Data in the Database” on page 706<br />

Defining the Persistence Unit<br />

As described in “Accessing Databases from Web Applications” on page 701, a persistence unit is<br />

defined by a persistence.xml file, which is packaged with the application WAR file. This file<br />

includes the following:<br />

■ A persistence element that identifies the schema that the descriptor validates against and<br />

includes a persistence-unit element.<br />

■ A persistence-unit element that identifies the name of a persistence unit and the<br />

transaction type.<br />

■ An optional description element.<br />

■ A jta-data-source element that specifies the global JNDI name of the JTA data source.<br />

<strong>The</strong> jta-data-source element indicates that the transactions in which the entity manager<br />

takes part are JTA transactions, meaning that transactions are managed by the container.<br />

Alternatively, you can use resource-local transactions, which are transactions controlled by the<br />

application itself. In general, web application developers will use JTA transactions so that they<br />

don’t need to manually manage the life cycle of the EntityManager instance.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

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

Saved successfully!

Ooh no, something went wrong!