Overview

This specification will define a RESTful API for dealing with JPA. The intent is to simplify how JPA persistence units can be accessed using REST with JSON or XML formatted messages. A JPA-RS runtime will provide access to all persistence units packaged in the same application that it is running in as well as any dynamic persistence unit that is provisioned within it.

Using JPA-RS

JPA-RS comes in two parts and is available starting with our 2.4.0 release.

Server side - The majority of code is written in EclipseLink and should be run server-side there are two ways you can get this functionality

If you are on a server that distributes with the full EclipseLink jar or on our 2.4.0 or 2.4.1 release, no changes are needed

If you are on our 2.4.2 or a later release and on a server that uses our OSGi bundles, you will have to ensure that you have the org.eclipse.persistence.dbws bundle (e.g. GlassFish ships with the OSGi bundles. You will need to ensure our org.eclipse.persistence.dbws bundle is available to GlassFish)

Client Side - A small web-fragment called org.eclipse.persistence.jpars is used to enable JPA-RS. This web-fragment should be included with your application.

Although there is no reason the web service cannot be enabled on any fairly recent container that contains Jersey, initial development and testing was done on GlassFish 3.1.2 and this description will assume you are using GlassFish 3.1.2 or better.

JPA-RS uses "/persistence" as its path. (i.e. The base URL for JPA-RS for a particular application is: http://<server>:<port>/<applicationName>/persistence)

Persistence Unit Operations

The JPA-RS URI structure then requires a persistence unit name: /persistence/{unit-name}. Assuming this is a valid persistence unit in the given JPA-RS application context the following high level operations are available.

BASE /persistence/{unit-name}

ENTITY: /persistence/{unit-name}/entity

QUERY: /persistence/{unit-name}/query

SINGLE_RESULT_QUERY: /persistence/{unit-name}/singleResultQuery

METADATA: /persistence/{unit-name}/metadata

Data Formats: JSON or XML

This REST interface deals with XML and JSON representations of the data equally well. The caller is responsible for using the HTTP header values to indicate the format of the content it is sending (Content-Type = application/json or application/xml) as well as indicating the format of the result it expects (Accept = application/json or application/xml).
NOTE: In many REST utilities the accept value is defaulted to application/xml making it the users responsibility to configure this value explicitly.

Logging

Messages related to JPA-RS operations are logged to a logger called org.eclipse.persistence.jpars. Most messages are logged at the FINE level. Exception stacks are logged at FINER.

Messages related to operations withing EntityManagers, EntityManagerFactories and JAXBContexts are logged in the same manner as other EclipseLink logging.

Deployment Requirements

Weaving is required for several features to work (providing relationships as links, editing relationships, dealing with LAZY x-1 relationships). You should either deploy to a Java EE compliant server or statically weave you classes.

Note: Lazy x-1 relationships are only supported when using JPA RS's default mapping strategy which returns those mappings as links by using read-only mappings for the JSON/XML support and providing the links through a weaved attribute.

Entity Operations: /persistence/{unit-name}/entity/*

Entity operations are those performed against a specific entity type within the persistence unit. The {type} value refers to the type name (descriptor alias).

Composite Key

Composite keys are supported. In the initial implementation, the character '+' will be reserved and not available for use in fields that represent keys. Composite keys will be separated using the '+' character and should be specified in an order cooresponding to java default sorting of the attribute names.

PUT is required to be itempotent. As a result, it will fail if called with an object that expects the server to provide an ID field. Typically this will occur if the metadata specifies a generated key and the field that contains that key is unpopulated.

Entity Representation

Entities in JPA-RS are represented in two ways.

As JPA Entities - The mappings the JPA Entities must be represented in the typical EclipseLink JPA fashion using either annotations or xml files. These mappings will be used to interact with the data source.

As JAXB/JSON - No specific mapping information is required here. By default, JPA-RS will use the JAXB-specification-defined defaults to map to JAXB/JSON. (EclipseLink's JAXB implementation provides a JSON option that we leverage here). You can optionally provide JAXB annotations on the classes to alter the way the objects are mapped. Additionally, the persistence unit property "eclipselink.jpa-rs.oxm" can be specified in your persistence unit's persistence.xml to specify xml-defined JAXB mappings.

Relationships

In general, JAXB default mappings are sufficient to allow information exchange using JSON/JAXB. There are, however, some special cases when dealing with relationships.

Bi-directional Relationships and Cycles

Bi-directional Relationships are quite typical in JPA and easy to represent in a database using foreign keys. They are much more difficult to represent in a default manner in a JSON or XML document using JAXB. EclipseLink's JAXB implementation provides a way to define an inverse relationship. Inverse relationships are not specifically written to XML/JSON, but are populated when the JSON/XML is unmarshalled. EclipseLink takes advantage of that functionality when it finds bidirectional relationships as follows:

JPA bidirectional relationships are defined to have an owning side and a non-owning side. JPA mapping provides a "mapped-by" attribute that defines which is which. JPA-RS defaults the owning side to be an inverse relationship. As a result, when an object with a owned-relationship is read or written that relationship will be ignored.

Cycles are more difficult to detect and JPA-RS requires that users provide JAXB mapping that allows us to resolve cycles in the cases where they cause problems. In general you will only see issues when you try to create a group of new objects that include a cycle. In order to handle that situation, you will have to choose one of the mappings in the cycle and map it as XMLTransient.

Pass By Reference/ Pass By Value

JPA-RS allows relationship objects to be passed either by value or by reference.

To pass an object by value, simply create typical JSON/XML that represents the object. The following JSON passes myA by value:

B {
id:11
myA {
id: 1
}
}

To pass an object by reference, a link is used. The link represents the JPA-RS call necessary to get that object. The following JSON passes myA by reference:

Security

Future Development

Multi-Tenancy in the URL

When using EclipseLink's multi-tenancy features developers can choose to isolate their tenant's data by schema/table (@Multitenant(TABLE_PER_TENANT) - EclipseLink 2.4) or within a shared table (@Multitenant(SINGLE_TABLE) - EclipseLink 2.3). In order to access a persistence unit that supports concurrent access by multiple tenants the persistence context must specify tenant discriminator values within its properties. To support this usage within JPA-RS the tenant identifier values will be supplied using matrix parameters within the persistence unit portion of the URI.