Skip to main content
Jump to: navigation, search

Difference between revisions of "EclipseLink/Development/2.4.0/JPA-RS/REST-API"

(Dynamic Configuration)
(Dynamic Configuration)
Line 377: Line 377:
When you have provided the configuration files, you can bootstrap the persistence unit with the Bootstrap REST call documented above.

Revision as of 11:24, 12 March 2012


History Date Author Description 01/10/11 dclarke Initial Version

Date Author Description
Oct 1, 2011 dclarke Initial Version
Nov 9, 2011 dclarke Moved to wiki for community review and feedback.
Jan-Mar, 2012 tware updates and additions to REST API descriptions


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.

URI Root : /persistence

The root URI for the RESTful interface is defined in the application's web.xml:

   <servlet-name>Jersey REST Service</servlet-name>

Here the root URI is 'persistence' to differentiate it from other services

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 give JPA-RS application context the following high level operations are available.

  • ENTITY: /persistence/{unit-name}/entity
  • QUERY: /persistence/{unit-name}/query
  • METAMODEL: /persistence/{unit-name}/metamodel
  • ADMIN: /persistence/{unit-name}

HTTP Method Basics

The HTTP methods used in JPA-RS with there basic interpretation in persistence are:

  • GET:
  • PUT: enclosed entity be stored under the supplied Request-URI
    • INSERT when the PK entity does not exist
    • UPDATE when the entity does exist
  • POST: new subordinate of resource identified by the request-URI

The HEAD, TRACE, and CONNECT methods currently have no defined meaning in JPA-RS

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). In cases where no header value is specified JSON will be used by default and in cases where content-type is specified and accept is not the returned format will match the content-type passed in. NOTE: In many REST utilities the accept value is defaulted to application/xml making it the users responsibility to configure this value explicitly.

Web Caching

In addition to the internal caching within TopLink (EclipseLink) the results from REST URI calls can be cached in various points between the initiating server and the user, including their browser. This caching is determined by URI structure and the HTTP header information in the response from the REST calls. Generally only GET call responses are cached so these must be addressed carefully to ensure the proper caching information is provided so that the end user of the RESTful persistence interface get the most correct information possible while still benefiting from web caching. TODO: Add guidelines

HTTP Header Fields

Standard HTTP

  • If-Match - conditional
  • Warning

EclipseLink JPA-RS

  • Refresh

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).


// TODO id should not be specified with query parameters. We should use Matrix parameters or the URL itself

EntityManager.find API:

public <T> T find(Class<T> entityClass, Object primaryKey);
public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties);

The JPA-RS GET with format

Composite Key


Result Caching



The EntityManager.refresh operation can be invoked using the find (GET) with the addition of the Refresh header field.




// TODO: POST should be itempotent, but merge can result in an insert with sequencing. We need to deal with this issue


Query Operations

  • Named Query: GET /persistence/{unit-name}/query/{name}{params}
  • Dynamic Query: GET /persistence/{unit-name}/query
    • Dynamic JPQL query in payload
    • Dynamic native query in payload

Named Query

Named queries doing reads can be run two ways in JPA. Both are supported in the REST API.

List of Results

Single Result

Update/Delete Query

Dynamic Query


Metamodel Operations

  • GET /persistence/{unit-name}/metamodel
  • GET /persistence/{unit-name}/metamodel/entity/{type}
  • GET /persistence/{unit-name}/metamodel/query/{name}

Admin Operations

The admin operations, which can be disabled in a given deployment, focus on persistence unit level administration operations:

  • Create
    • Dynamic Persistence Unit: PUT: /persistence/{unit-name}
    • Add Dynamic Type: POST: /persistence/{unit-name}/
  • Delete Persistence Unit: DELETE /persistence/{unit-name}

Create Dynamic Persistence Unit

Creating a dynamic persistence unit within a JPA-RS runtime involves passing in the necessary EclipseLink metadata and having the PU realized using dynamic entities.

  • PUT /persistence/{unit-name}
  • Payload: Key/Value or XML - pick one
    • persistenceXmlURL = <URL>
    • full text of persistence.xml
  • Response:
    • CREATED - on success
    • NOT_FOUND - on failure

Bootstrap from stored Persistence Units

This option will start the REST service, look in a datastore for any stored persistence units and make those available. Any persistence units that are bootstrapped will be added to the datastore

  • PUT /persistence
  • Payload: Key/Value
    • datasourceName = DatasourceJNDIName
  • Response:
    • CREATED - on success
    • NOT_FOUND - on failure

List Existing Persistence Units

List Types in a Persistence Unit

Delete Persistence Unit

Using JPA-RS

JPA-RS comes packaged in a single Jar. The key piece of functionality is a web service found in a class called: Although there is no reason the web service cannot be enabled on any fairly recent container, 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.


In order to deploy the JPA RS, it must be somehow deployed as part of your web application. The simplest way to do that is to package it in a war file. The key things you have to include are as follows:

A web.xml file pointing at the web service. Here is an example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
    <servlet-name>JPA-RS Service</servlet-name>
    <servlet-name>JPA-RS Service</servlet-name>

A beans.xml file. It is required, but does not need any specific contents.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

Defining Metadata

JPA-RS can make use of a standard JPA persistence unit, or can be bootstrapped with dynamically provided metadata.

Standard Persistence Unit

To use a standard JPA persistence unit, simply deploy the JPA-RS jar in such a way as it can see the persistence unit. If you have done that, no further configuration or bootstrapping steps are required. You can interact with the persistence unit through the REST API immediately.

Dynamic Configuration

JPA-RS can be bootstrapped dynamically with no java code or other persistence-unit-specific configuration in its deployment artifact. There are two steps to doing that.

First, you must define your metadata. The metadata is provided using two JPA-standard configuration files.

1. persistence.xml - The persistence.xml configures the persistence unit. You will provide two properties that allow the dynamic functionality to work in this file:

  • <property name="eclipselink.metadata-source" value="XML"/>
  • <property name="eclipselink.metadata-source.xml.url" value="**URL OF YOUR ORM.XML FILE(("/>

Note: This persistence.xml is fairly standard except that you must keep in mind the fact that the only deployment artifacts you have are it and the orm.xml file described below, so elements like <mapping-file> and <jar-file> will only work if you find a way to make them available on the classpath. In addition

2. orm.xml - This defines the structure of your data both in the database and as it is passed through REST. It is an extended version of the orm.xml file defined in the JPA specification. The key extension you will use is the ability to map an entity with access type= VIRTUAL. The VIRTUAL access type is what allows EclipseLink to build a persistence unit without java classes. You will have to make your orm.xml available through a URL so it can be referred to in the persistence.xml as described above.

Here are some sample files:

The following persistence.xml points at a single dynamic class called Auction and gives a URL for where the mappings are defined.

    <persistence-unit name="auction" transaction-type="JTA">

        	<property name="eclipselink.metadata-source" value="XML"/>
        	<property name="eclipselink.metadata-source.xml.url" value="http://localhost:8080/JPA-RS-Auction/xmlconfig/auction-orm.xml"/>
            <property name="" value="SunAS9"/>

The following orm.xml defines the mappings for a single Entity called Auction and additionally provides a query that can retrieve all the Auctions.

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.3"
    <named-query name="Auction.all">
        <query>SELECT a FROM Auction a</query>

    <entity class="Auction" access="VIRTUAL">
        <table name="AUCTION_AUCTION" />
            <id name="id" attribute-type="Integer">
                <column name="ID" />
            <basic name="name" attribute-type="String" />
            <basic name="image" attribute-type="String"/>
            <basic name="description" attribute-type="String">
                <column column-definition="CLOB"/>
            <basic name="startPrice" attribute-type="Double"/>
            <basic name="endPrice" attribute-type="Double"/>
            <basic name="sold" attribute-type="boolean"/>

When you have provided the configuration files, you can bootstrap the persistence unit with the Bootstrap REST call documented above.

Back to the top