A standardized object-relational mapping mechanism for the Java platform

An introduction to the Java Persistence API

The Java Persistence API was introduced as part of the Enterprise JavaBeans (EJB) 3.0 specification in Java Platform, Enterprise Edition 5 (Java EE). The Java Persistence API draws on ideas from leading persistence frameworks and APIs such as Hibernate, Oracle TopLink, Java Data Objects, (JDO) and EJB container-managed persistence.

The highlights of the Java Persistence API are:

  1. A standardized mechanism for object-relational mapping for both Java EE and Java Platform, Standard Edition (Java SE)
  2. A model based completely on plain-old Java objects (POJO)
  3. Fewer classes and interfaces required
  4. Optional deployment descriptors
  5. Easy relationships declarations
  6. Support for inheritance and polymorphism
  7. Support for persistence outside the EJB container
  8. Java Persistence query language (JPQL), with a richer set of features as compared to the EJB QL of the earlier EJB specification

This story walks you through the Java Persistence API's features, using several examples for illustration.

Java Persistence API: A look deeper

The Java Persistence API is a lightweight, POJO-based framework for object-relational mapping. The mapping between Java objects and a relational database is achieved with the help of Java language metadata annotations and/or XML deployment descriptors (usually XML descriptors are optional). The Java Persistence API heavily depends on the metadata annotations.

The API consists of:

  • Java Persistence API
  • The Java Persistence query language
  • Metadata annotations

Entities: Persistent objects

An entity is a lightweight persistence domain object, coded as a POJO, and marked as an entity with the @Entity (javax.persistence.Entity) annotation. To write entity classes, the javax.persistence package is used.

An entity represents a table in a relational database. Each entity instance corresponds to a row in the table. An entity's state is represented either through persistent fields or persistent properties. The entity fields or properties represent the columns in the table.

An entity's persistence state can be accessed either through the entity's instance variables or through the property accessor methods (JavaBeans-style getter/setter methods) in the entity classes. These instance variables/methods use annotations to describe the relationships among the entities - such as, the relationships in the relational database. By default, all properties/fields are persisted into the datastore, except those marked with the @Transient annotation.

Let's take the example of a Dept table that has three columns, ID, name and location, out of which the column id is a primary key. The following code shows the corresponding entity for the domain object Dept:

    package university;

   import java.io.Serializable;
   import javax.persistence.Entity;
   import javax.persistence.Table;
   import javax.persistence.Column;
   import javax.persistence.Id;

   @Entity
   @Table (name="Dept")
   public class Department implements Serializable {
      @Id
      @Column(name="id", nullable=false)
      private String deptId;
      @Column(name="name")
      private String deptName;
      private String location;

      public void setDeptId(String deptId) {
         this.deptId = deptId;
      }

      public String getDeptId() {
         return this.deptId;
      }

      public void setDeptName(String deptName) {
         this.deptName = deptName;
      }

      public String getDeptName() {
         return this.deptName;
      }

      public void setLocation(String location) {
         this.location = location;
      }

      public String getLocation() {
         return location;
      }
      ..................
      ..................
   }

In the above example, to mark a simple POJO-based class, the @Entity annotation is used. The table represented by the entity is denoted using the @Tableannotation. If the entity name is the same as that of the table name, @Table is not required. In many cases, defaults are assumed when an annotation is not specified. Entity classes must have a public/protected no-argument constructor.

The @Column annotation specifies the database column that corresponds to the property/field name. Again, if a property/field is not annotated with @Column, the column name is assumed to be the same as that of the property/field name in the persistence storage.

The primary key in the entity class can be specified by marking an appropriate persistent property/field with the @Id annotation. The above example uses field-based access. That means the annotation @Id has been used with the deptIdfield. To use property-based access, the annotation @Id has to be placed with the getter/setter methods as demonstrated below:

   ..................
      @Id
      public String getDeptId() {
         return this.deptId;
      }

      public void setDeptId(String deptId) {
         this.deptId = deptId;
      }
      ..................

When entities have composite primary keys, a primary key class must be defined and annotated using @EmbeddedId (javax. persitence.EmbeddedId) and @IdClass (javax.persistence.IdClass ) annotations.

An entity class may use other fine-grained classes to represent the entity state. These classes are called embeddable classes and do not have any persistent entity of their own. They exist only as embedded objects of the entity to which they belong. Embeddable classes are similar to entity classes, but are annotated with @Embeddable (javax.persistence.Embeddable). For example, the object Address can be considered as an embeddable object for the entity Student.

1 2 3 Page 1
Page 1 of 3