DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Why I Prefer Trunk-Based Development
  • Using Render Log Streams to Log to Papertrail
  • Database Integration Tests With Spring Boot and Testcontainers
  • The SPACE Framework for Developer Productivity

Trending

  • Why I Prefer Trunk-Based Development
  • Using Render Log Streams to Log to Papertrail
  • Database Integration Tests With Spring Boot and Testcontainers
  • The SPACE Framework for Developer Productivity
  1. DZone
  2. Data Engineering
  3. Databases
  4. Introducing EclipseLink JPA-RS

Introducing EclipseLink JPA-RS

Blaise Doughan user avatar by
Blaise Doughan
·
Apr. 20, 13 · Interview
Like (0)
Save
Tweet
Share
14.06K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous series of posts I covered how to create a JAX-RS service that leveraged JPA for the persistence layer.  EclipseLink contains a component called JPA-RS that can be used to easily and automatically expose a persistence unit as RESTful service (that supports XML and JSON messages).  MOXy provides the XML and JSON-binding for JPA-RS and things like bidirectional mappings are automatically mapped for you.  In another post I cover how MOXy can be used to customize the messages shown in this example.

I will use the JPA model that I created in the posts below:

  • Part 1 - The Database
  • Part 2 - Mapping the Database to JPA Entities
    • Annotations
    • XML Metadata
Packaging/Deployment 

Using JPA-RS is a simple matter of packaging.  We will create a WAR that contains our JPA model in a JAR, the JPA-RS JAR, and a simple session bean to initialize our JPA model.  For this example I am using a promoted build of GlassFish 4.0 that contains EclipseLink 2.5.
  • http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/

CustomerJPARS.war
  • WEB-INF
    • classes
      • org
        • example
          • ejb
            • PersistenceWeaverBean.class
    • lib
      • CustomerJPA.jar
      • org.eclipse.persistence.jpars_2.5.0.qualifier.jar
  • META-INF
    • MANIFEST.MF 

PersistenceWeaverBean 
JPA-RS requires that the JPA entities have been initialized.  We'll create a simple session bean to accomplish this.
package org.example.ejb;
 
import javax.ejb.*;
import javax.persistence.*;
 
@Startup
@Singleton
@LocalBean
public class PersistenceWeaverBean {
 
    @SuppressWarnings("unused")
    @PersistenceUnit(unitName = "CustomerService")
    private EntityManagerFactory emf;
 
}

CustomerJPA.jar
This JAR contains the JPA model that we defined in the following post: 
  • Part 2 - Mapping the Database to JPA Entities
    • Annotations
    • XML Metadata
org.eclipse.persistence.jpars_2.5.0.qualifier.jar

This is the JPA-RS JAR that comes from the EclipseLink install:
<ECLIPSELINK_HOME>/jlib/jpa/org.eclipse.persistence.jpars_2.5.0.qualifier.jar
What Can I Do with My Service? (Service Metadata)

As soon as we have deployed the WAR our service is active.  We can perform a GET to see the metadata for our service.

GET (application/xml or application/json)

The URI to get the metadata for a service is of the following format:
http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/metadata

Below is the URI for our example:
http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata
Response

Below is the metadata for our service.  In addition to the persistence unit name, we see links to the metadata for each of the entities in our JPA model.  Next we will take a closer look at the Customer entity.
{
    "persistenceUnitName": "CustomerService",
    "types": [
        {
            "_link": {
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Address",
                "method": "application/json",
                "rel": "Address"
            }
        },
        {
            "_link": {
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/PhoneNumber",
                "method": "application/json",
                "rel": "PhoneNumber"
            }
        },
        {
            "_link": {
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Customer",
                "method": "application/json",
                "rel": "Customer"
            }
        }
    ]
}

What Can I Do with An Entity? (Entity Metadata) 

If we follow the link for one of the entities then we get the following information:
  • The entities attributes.
  • The CRUD operations that we can perform on the entity.
  • The named queries that we can perform on the entity.

GET (application/xml or application/json)

The URI to get the metadata for an entity is of the following format:
http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit/metadata/entity/{Entity}
Below is the URI to get the metadata for the Customer entity:
http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Customer

Response 

Below is the metadata for the Customer entity.  We will be taking a closer look at the POST operation (lines 37-39) and the named query (lines 49-58).
{
    "name": "Customer",
    "attributes": [
        {
            "name": "id",
            "type": "long"
        },
        {
            "name": "firstName",
            "type": "String"
        },
        {
            "name": "lastName",
            "type": "String"
        },
        {
            "name": "address",
            "type": "Address"
        },
        {
            "name": "phoneNumbers",
            "type": "Set<PhoneNumber>"
        }
    ],
    "linkTemplates": [
        {
            "method": "get",
            "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/{primaryKey}",
            "rel": "find"
        },
        {
            "method": "put",
            "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer",
            "rel": "persist"
        },
        {
            "method": "post",
            "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer",
            "rel": "update"
        },
        {
            "method": "delete",
            "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/{primaryKey}",
            "rel": "delete"
        }
    ],
    "queries": [
        {
            "queryName": "findCustomersByCity",
            "returnTypes": [
                "Customer"
            ],
            "linkTemplate": {
                "method": "get",
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/query/findCustomersByCity;city={city}",
                "rel": "execute"
            },
            "jpql": "SELECT c FROM Customer c WHERE c.address.city = :city"
        }
    ]
}
Persisting an Entity

We will use the POST operation to create a new instance of the Customer entity.

POST (application/xml or application/json)

The URI to create an entity is of the following format:
http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/entity/{Entity}

Below is the URI to create an instance of Customer:
http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer

Request 

Below is the JSON representation of our Customer data that we will post to the above URI:
{
    "id" : 1,
    "firstName" : "Jane",
    "lastName" : "Doe",
    "address" : {
        "id" : 1,
        "street" : "1 A Street",
        "city" : "Any Town"
    },
    "phoneNumbers" : [{
        "id" : 2,
        "type" : "work",
        "num" : "555-1111"
    }, {
        "id" : 3,
        "type" : "home",
        "num" : "555-2222"
    }]
}
Performing a Query

JPA-RS automatically creates URIs for each of the named queries that we defined in our JPA model:

GET (application/xml or application/json)

The URI to execute a named query is of the following format:
http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/query/{NamedQuery;Parameters}
Below we will call the findCustomersByCity named query to find all customers from Any Town.
http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/query/findCustomersByCity;city=Any%20Town

Response

Below is the result of calling the named query.  We can see that relationships between entities are represented as links.  You perform a GET on the link to get the referenced data.
[
    {
        "firstName": "Jane",
        "id": 1,
        "lastName": "Doe",
        "_relationships": [
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/1/address",
                    "rel": "address"
                }
            },
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/1/phoneNumbers",
                    "rel": "phoneNumbers"
                }
            }
        ],
        "address": {
            "_link": {
                "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Address/1",
                "method": "GET",
                "rel": "self"
            }
        },
        "phoneNumbers": [
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/PhoneNumber/3",
                    "method": "GET",
                    "rel": "self"
                }
            },
            {
                "_link": {
                    "href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/PhoneNumber/2",
                    "method": "GET",
                    "rel": "self"
                }
            }
        ]
    }
]

Further Reading 

If you enjoyed this post then you may also be interested in: 
  • http://wiki.eclipse.org/EclipseLink/Examples/JPA
  • Creating a RESTful Service
    • Part 1 - The Database
    • Part 2 - Mapping the Database to JPA Entities
      • Annotations
      • XML Metadata
    • Part 3 - Mapping JPA entities to XML (using JAXB)
    • Part 4 - The RESTful Service
    • Part 5 - The Client
  • MOXy as Your JAX-RS JSON Provider - MOXyJsonProvider



 
Database EclipseLink Metadata POST (HTTP) Uniform Resource Identifier

Published at DZone with permission of Blaise Doughan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Why I Prefer Trunk-Based Development
  • Using Render Log Streams to Log to Papertrail
  • Database Integration Tests With Spring Boot and Testcontainers
  • The SPACE Framework for Developer Productivity

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: