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.
The document is organized as follows.
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 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 deployment descriptor information is provided using
Observe various annotations that represent various aspects of the entity in the class.
@Entity : Marks the ShareAccount class as an entity that can be persisted entirely in the database.
@Table(name = "SHAREACCOUNT") : Designates the database table SHAREACCOUNT to the entity. The columns in the database table map to the attributes of ShareAccount entity.
@Id : This annotation designates primary key to the entity. In this case, it is the shareAccountNo attribute of the entity class.
@NamedQuery(name="findAccounts", query="SELECT a FROM ShareAccount a") : This annotation declares a named query by name findAccounts. This query retrieves all the share accounts from the database. Later in the ShareHolderBean, the NamedQuery is executed by using the name findAccounts.
@Version : The version attribute of the ShareAccount entity.
The annotations placed around some of the methods of the ShareAccount class are @PrePersist, @PreUpdate, @PostUpdate and @PostLoad. These are the callback methods called by persistence container when corresponding database operations are performed on the entity.
ShareHolder.java is the remote interface of the stateless session bean.
ShareHolderBean.java is the stateless session bean that uses JPA to perform database operations using ShareAccount entity.
Persistence.xml is the persistence deployment descriptor for the ejb module. It provides database connection information and declares entity classes among other things.
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.
Inheritance relationship in entities
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
In this technique, a single database table is designated for entire entity class hierarchy exhibiting inheritance relationship. For example, if entity C extends entity B which extends entity A, the fields of all the entities are stored in a single table. The entities A, B and C can be persisted/updated/deleted/retrieved independently. This technique requires all the columns except for primary columns and columns of parent most entity A, should be nullable. This is because, suppose take the case when an entity B is being inserted. The entity B will have the values for the fields of A and itself but no values for fields of C. Since entire hierarchy uses a single table, the columns pertaining to entity C must be nullable. The same case arises when inserting entity A.
The below example illustrates the use this technique. The enterprise application has 3 entities and uses a single database table to store these entities. A stateless session bean uses JPA to perform DML operations on these entities. The web client looks up the SLSB trigger the operations.
@DiscriminatorColumn annotation designates table column to be used to discriminate rows that correspond to different entities in the class hierarchy. @DiscriminatorValue annotation provides value that corresponds to the Account entity for the DiscriminatorColumn. The value is Account. Note that the Account entity class is declared as an abstract class. This is to prevent applications from instantiating Account objects. The intention is, to allow instantiating only sub entities SavingsAccount and CurrentAccount as given below.
@DiscriminatorValue annotation provides value that corresponds to the SavingsAccount entity for the DiscriminatorColumn. The value is SAVINGSACCOUNT
@DiscriminatorValue annotation provides value that corresponds to the SavingsAccount entity for the DiscriminatorColumn. The value is CURRENTACCOUNT.
The CurrentAccount entity has currentAccountRules field and SavingsAccount has SavingsAccountRules field. Both inherit fields from the parent entity Account
The above servlet client only inserts entities in the table ACCOUNT. The code that deletes entities is commented out. We can write variety of clients that lookup the EJB and perform operations on the entities.
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.
- Run the servlet with the following input
- The following data will be inserted into ACCOUNT table.
Note that JPA has inserted the value CURRENTACCOUNT in the column ACCOUNTTYPE for CurrentAccount entities and inserted the value SAVINGSACCOUNT for SavingsAccount entites.