Wiki Markup |
---|
{scrollbar} |
Java Persistence API
...
The Java Persistence API is a new programming model under EJB3.0 specification (JSR220) for the management of persistence and object/relational mapping with Java EE and Java SE. With JPA, developers can easily develop java applications that perform operations on relational database management systems using java objects and mapping. In that way, java applications developed using JPA are not only portable across different platforms, but also applications can be easily developed using simple yet powerful programming model provided by JPA. This greatly improves application maintainability against ever changing database world. JPA insulates applications from all the complexity and non-portable boilerplate code involved in database connectivity and operations.
Apache geronimo uses OpenJPA for providing Java Persistence API to Java EE applications deployed in the server. Below sections illustrate developing applications using JPA and how to write various deployment descriptors and plans for apache geronimo.
...
This example illustrates developing an enterprise application that uses JPA for persistence. The database used is the embedded derby shipped with apache geronimo. Here, we present a persistence deployment plan descriptor (persistence.xml
) that contains database connectivity and other information for the application. The persistence.xml
is placed under META-INF/
directory of the application archive. The application contains an ejb module and a web module. Ejb module uses a stateless session bean ShareHolderBean
that uses JPA to perform database operations on the table SHAREACCOUNT
in the ShareDB
derby database. The SHAREACCOUNT
table contains information about each shareholder along with the information regarding number shares he or she possesses currently in the account. The ShareHolderBean
has methods that retrieve shareholder information, buy/sell shares of a particular shareholder, close the shareholder account etc. The web application has a servlet that looks up the {{ ShareHolderBean
}} and trigger the operations on it. The deployment descriptor information for the ejb module is provided using Java EE annotations in the respective bean classes. However, the persistence plan deployment descriptor information is provided using META-INF/persistence.xml
file.
...
The Persistence.xml
is the persistence plan deployment descriptor for the ejb module. It provides database connection information and declares entity classes among other things.
Note |
---|
The default namespace of the above XML document is http://java.sun.com/xml/ns/persistence. The XML elements that do not have a namespace prefix belong to the default namespace. |
...
The Servlet client Test.java
, looks up the ShareHolderBean
and executes various methods on the ShareAccount
entity. When the url http://localhost:8080/ShareHolderWEB/Test is hit on a browser window, the following output is displayed.
...
Entities can exhibit inheritance relationship among themselves. The entities involved in inheritance relationship can be persisted/updated/retrieved independently. There are several ways of realizing inheritance relationship among entities. There are as follows.
- Single database table per class hierarchy
- Separate database table per subclass
- Single database table per concrete entity class
Single database table per class hierarchy
...
The persistence schema uses AccountDB
derby database. It declares the entities in the persistence unit as well. The following procedure explains how to deploy/run the sample.*
- Create an EAR application that contains an EJB application packaging all entity classes, ejb classes and META-INF/persistence.xml
...
- Create META-INF/ejb-jar.xml. Since we have used annotations, we do not have to provide any declarations in it.
...
- Create a WEB application in the EAR and add the above servlet.
...
- Create a derby database by name
AccountDB
using admin console.
...
- Create a table by name
ACCOUNT
as following.Code Block SQL SQL borderStyle solid title ACCOUNT table create table ACCOUNT(ACCOUNTNO integer, ACCOUNTTYPE varchar(50), NAME varchar(50), ADDRESS varchar(225), BRANCHCODE integer, INTERESTRATE decimal(15,2), MINBALANCE decimal(15,2), BALANCE decimal(15,2), CURRENTACCOUNTRULES varchar(225), SAVINGSACCOUNTRULES varchar(225))
...
- Run the servlet with the following input
http://localhost:8080/<web-context>/Test?accNo=1&name=Joe
http://localhost:8080/<web-context>/Test?accNo=3&name=John
- The following data will be inserted into
ACCOUNT
table
...
- .
No Format borderStyle solid title ACCOUNT table ACCOUNTNO ACCOUNTTYPE NAME ADDRESS BRANCHCODE INTERESTRATE MINBALANCE BALANCE CURRENTACCOUNTRULES SAVINGSACCOUNTRULES 1 CURRENTACCOUNT Joe Joe's address 10 20.00 200.00 5000.00 Current Account!! 2 SAVINGSACCOUNT Joe Joe's address 12 234.00 4534.00 13323.00 Savings Account!! 3 CURRENTACCOUNT John John's address 10 20.00 200.00 5000.00 Current Account!! 4 SAVINGSACCOUNT John John's address 12 234.00 4534.00 13323.00 Savings Account!!
Note that JPA has inserted the value CURRENTACCOUNT
in the column ACCOUNTTYPE
for CurrentAccount entities and inserted the value {SAVINGSACCOUNT
}} for SavingsAccount entites.