First Look at Java Persistence API (JPA)

The Java Persistence API page on java.sun.com describes the Persistence API as:

The Java Persistence API provides a POJO persistence model for object-relational mapping. The Java Persistence API was developed by the EJB 3.0 software expert group as part of JSR 220, but its use is not limited to EJB software components. It can also be used directly by web applications and application clients, and even outside the Java EE platform, for example, in Java SE applications

So let’s look a bit deeper into the features of new API.

Persistence is dealing with entities. An Entitiy is a lightweight domain specific object (in java) that sometimes should be stored to persistent storage of some kind (e.g. Relational Database).  Typically an entity is represented by a Databae table in a relational databases and each entity instance corresponds with a row in that table.

In the Java Persistence API,  persistent state of an entity is represented either through persistent fields or persistent properties. These fields  use object-relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store.

So Entities are marked with the @Entity (javax.persistence.Entity) annotation and the entity-class should implement Serializable interface. Further requirements for entitiy classes can be found in the Java EE 5 Tutorial.

Here is  just little examle of Persisten entitiy “User”.

package de.holbreich.chopping;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Id;

@Entity
@Table (name="User")
public class Userimplements Serializable {
 @Id
 @Column(name="id", nullable=false)
 private String userId;

 @Column(name="Username")
 private String username;

 @Column(name="Firstname")
 private String firstname

 public void setUserId(String userId){
 this.userId= userId;
 }

 public String getUserId() {
 return this.userId;
 }

 public void setUsername(String username) {
 this.username=username;
 }

 public String getUsername() {
 return this.username;
 }
...
}

If the entity name is the same as that of the table name, @Table is not required. The same with @Column Attribute. Not Persistent Fields or methods should be marked with @Transient. Here more Information about persistent field and properties and especially Primary Keys.
So far its quite simple.

Entities can have relations to each other. Java Persistence API knows four types of multiplicities: one-to-one, one-to-many, many-to-one, and many-to-many, which are marked with appropriate anotations. Furthermore relationships can be unidirectional or bidirectional. In a unidirectional relationship, only one entity has a relationship field or property that refers to the other. Read more about it.

This looks really interesting to me. But it gone to be better then, the Java Persistence API has an important new feature that was not covered bu SUN before: support for inheritance and polymorphism. An entity may inherit from another entity class. By default, the queries are polymorphic and are applicable against the entire entity hierarchy. Read more about inheritance posibilities.

An finely there is a new and better Query language

Queries and query language

The Java Persistence query language (JPQL) is an extension of the EJB QL, but overcomes EJB QL’s limitations and provides more features such as:

More on JPQL in the Tutorial

Management of entities

Entities are managed by the entity manager. The entity manager is represented by javax.persistence.EntityManager instances. Each EntityManager instance is associated with a persistence context. A persistence context is a set of managed entity instances that exist in a particular data store and defines the scope under which particular entity instances are created, persisted, and removed.

As expected the are two kinds of managers an container managed and one application managed.
With a container-managed entity manager, an EntityManager instance’s persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.


@PersistenceContext(unitName="user")
EntityManager em;

With application-managed entity managers, on the other hand, the persistence context is not propagated to application components, and the life-cycle of EntityManager instances is managed by the application.


@PersistenceUnit(unitName="user")
EntityManagerFactory emf;
EntityManager em = emf.createEntityManager();

Persistent context is described in persistense.xml file

Conclusion

I thik the Java Persistence API is an real cool standard, hope start use it soon. Here are the key features of it summarized:

Furthermore Java Persistence API can be used for Java SE environments too. This is possible because of the Java Persistence API’s support outside the EJB container.

  1. Java Persistence API on java.sun.com
  2. Java Persistence API in the Java EE 5 Tototial
  3. Good Article goes detailed on inheritance at JavaWorld
  4. Extended fetures in Examples of using Persistence API in EJB tier
  5. Using Persistens with JSF
  6. Java WebStart Persistence

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)