Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Practical RESTful Persistence
with EclipseLink JPA-RS
Shaun Smith
Oracle

#DV13 #JPARS

@shaunMsmith
•
•
•
•

JPA 2.0 (EE 6) and 2.1 (EE 7) reference implementation
JPA provider in GlassFish and Oracle WebLogic
Project foun...
Browser

Database

#DV13 #JPARS

@shaunMsmith
Browser

Java

Database

#DV13 #JPARS

@shaunMsmith
Browser

Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS

Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS
EclipseLink JAXB / JSON Binding
Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS
EclipseLink MOXy
Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS
EclipseLink MOXy
Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
Browser
HTML5

JAX-RS
EclipseLink MOXy
Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS
EclipseLink MOXy
{
id: 5,
…
}

Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
{
id: 5,
…
}

JAX-RS
EclipseLink MOXy
Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS
EclipseLink MOXy
Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS
EclipseLink MOXy
Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS
Binding Persistence
EclipseLink MOXy
XML / JSON

Java
Java Persistence
EclipseLink JPA
Relational / NoSQL

...
Browser
JAX-RS
Binding Persistence
EclipseLink MOXy
XML / JSON

Java
Java Persistence
EclipseLink JPA
Relational / NoSQL

...
EclipseLink MOXy
•
•
•

JAXB for Java/XML binding—covert Java to/from XML
Java/JSON binding—convert Java to/from JSON
Curr...
XML and JSON from JAXB Mappings

{
"phone-numbers" : [ {
"id" : 2,
"num" : "512-555-9999",
"type" : "mobile"
} ],
"address...
Challenges – Binding JPA Entities to
XML/JSON
<customer>
<phone-numbers>
<phone-number>
<id>1</id>
...
<type>mobile</type>...
Bidirectional Relationship
@Entity
public class Customer{
...
@OneToMany(mappedBy="owner")
private List<Phone> phones;
}
@...
What about @XmlTransient?
@Entity
public class Customer{
...
@OneToMany(mappedBy=“owner")
private List<Phone> phones;
}
@E...
With @XmlTransient
Loses the relationship!

owner

Customer
phones

#DV13 #JPARS

Phone

<customer>
<phone-numbers>
<phone...
EclipseLink XmlInverseReference
@Entity
public class Customer{
...
@OneToMany(mappedBy=“owner")
private List<Phone> phones...
With @XmlInverseReference
EclipseLink restores relationships on unmarshall!

owner

Customer
phones

#DV13 #JPARS

Phone

...
Demo
#DV13 #JPARS

@shaunMsmith
Browser
JAX-RS
JAX-RS
Binding Persistence
EclipseLink MOXy
XML / JSON

Java
Java Persistence
EclipseLink JPA
Relational / ...
JAX-RS with JPA Example
public class InvoiceService {...

public Invoice read(int id) {
return null;
}
...
#DV13 #JPARS

@...
JAX-RS with JPA Example
@Stateless
public class InvoiceService {...

public Invoice read(int id) {
return entityManager.fi...
JAX-RS with JPA Example
@Path("/invoice")
@Stateless
public class InvoiceService {...

public Invoice read(int id) {
retur...
JAX-RS with JPA Example
@Path("/invoice")
@Stateless
public class InvoiceService {...

@Path("{id}”)
public Invoice read(@...
JAX-RS with JPA Example
@Path("/invoice")
@Stateless
public class InvoiceService {...
@GET
@Path("{id}”)
public Invoice re...
JAX-RS with JPA Example
@Path("/invoice")
@Stateless
public class InvoiceService {...
@Produces({"application/xml"})
@GET
...
JAX-RS with JPA

GET http://.../invoice/4
GET http://.../invoice/4
http://.../invoice/4 mapped to
http://.../invoice/4 map...
Browser
JAX-RS
JAX-RS
EclipseLink MOXy
EclipseLink MOXy
Java
Java
EclipseLink JPA
EclipseLink JPA
JPA-RS
JPA-RS
Database

...
EclipseLink JPA-RS
•

Automatically provides REST operations for entities in
persistence unit (GET, PUT, POST, DELETE)

•
...
EclipseLink JPA-RS
•

Supports invocation of named queries via HTTP

•

Server side caching—EclipseLink clustered cache

•...
{

"id": 2,
"city": ”Toronto",
…
}

...

Browser

"id": 1,
"lastName": "Smith",
…
"address": {
"_link": {
"href": "http:/…...
Configuring JPA-RS
•

Add JPA-RS web fragment jar to WEB-INF/lib!

#DV13 #JPARS

@shaunMsmith
JPA-RS

GET http://.../persistence/Accounting/Invoice/4
GET http://.../persistence/Accounting/Invoice/4
JAX-RS http://.../...
Demo
#DV13 #JPARS

@shaunMsmith
Demo Model

#DV13 #JPARS

@shaunMsmith
EclipseLink JPA-RS Roadmap
•

Resource Model

• Scope of resource
• Entity format
• partial entities/projections
• Configu...
Browser
JAX-RS
EclipseLink MOXy
Java
EclipseLink JPA
Database

#DV13 #JPARS

@shaunMsmith
Summary
•
•
•

Java EE stack has (most of) what you need to build Thin Server
Architecture applications
You have to be awa...
The preceding is intended to outline our general product
direction. It is intended for information purposes only, and may
...
Upcoming SlideShare
Loading in …5
×

Practical RESTful Persistence

2,112 views

Published on

Java EE developers are increasingly required to embrace thin-server-architecture client application development while leveraging their existing components. On the surface, this appears to simply mean using JAX-RS to expose RESTful services. But the real challenges are often misunderstood and underestimated. EclipseLink has solved a large part of this problem through its new JPA-RS feature which integrates JAX-RS, JAXB and JPA to allow developers to expose their persistent entities over REST with support for all lifecycle operations and query execution. In building JPA-RS, EclipseLink faced and overcame many of the challenges developers face when realizing a REST resource model, addressing HATEOS, and the infrastructure for JSON and XML binding.

Published in: Technology, Education
  • Be the first to comment

Practical RESTful Persistence

  1. 1. Practical RESTful Persistence with EclipseLink JPA-RS Shaun Smith Oracle #DV13 #JPARS @shaunMsmith
  2. 2. • • • • JPA 2.0 (EE 6) and 2.1 (EE 7) reference implementation JPA provider in GlassFish and Oracle WebLogic Project founded on mature Oracle TopLink codebase EPL and EDL licensed open source JPA and JAXB implementation #DV13 #JPARS @shaunMsmith
  3. 3. Browser Database #DV13 #JPARS @shaunMsmith
  4. 4. Browser Java Database #DV13 #JPARS @shaunMsmith
  5. 5. Browser Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  6. 6. Browser JAX-RS Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  7. 7. Browser JAX-RS EclipseLink JAXB / JSON Binding Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  8. 8. Browser JAX-RS EclipseLink MOXy Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  9. 9. Browser JAX-RS EclipseLink MOXy Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  10. 10. Browser Browser HTML5 JAX-RS EclipseLink MOXy Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  11. 11. Browser JAX-RS EclipseLink MOXy { id: 5, … } Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  12. 12. Browser { id: 5, … } JAX-RS EclipseLink MOXy Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  13. 13. Browser JAX-RS EclipseLink MOXy Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  14. 14. Browser JAX-RS EclipseLink MOXy Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  15. 15. Browser JAX-RS Binding Persistence EclipseLink MOXy XML / JSON Java Java Persistence EclipseLink JPA Relational / NoSQL Database #DV13 #JPARS @shaunMsmith
  16. 16. Browser JAX-RS Binding Persistence EclipseLink MOXy XML / JSON Java Java Persistence EclipseLink JPA Relational / NoSQL Database #DV13 #JPARS @shaunMsmith
  17. 17. EclipseLink MOXy • • • JAXB for Java/XML binding—covert Java to/from XML Java/JSON binding—convert Java to/from JSON Currently no Java/JSON binding standard • Java API for JSON Processing (JSR 535) is parsing, not binding • EclipseLink interprets JAXB XML bindings for JSON • Content-type selectable by setting property on Marshaller/Unmarshaller #DV13 #JPARS @shaunMsmith
  18. 18. XML and JSON from JAXB Mappings { "phone-numbers" : [ { "id" : 2, "num" : "512-555-9999", "type" : "mobile" } ], "address" : { "city" : "New York", "id" : 1, "street" : "Central Park East" }, "firstName" : "Woody", "id" : 1, "lastName" : “Allen" <?xml version="1.0" encoding="UTF-8"?> <customer> <phone-numbers> <phone-number> <id>2</id> <num>512-555-1234</num> <type>home</type> </phone-number> </phone-numbers> <address> <city>New York</city> <id>1</id> <street>Central Park East</street> </address> <firstName>Bill</firstName> <id>1</id> <lastName>Allen</lastName> </customer> } #DV13 #JPARS @shaunMsmith
  19. 19. Challenges – Binding JPA Entities to XML/JSON <customer> <phone-numbers> <phone-number> <id>1</id> ... <type>mobile</type> </phone-number> </phone-numbers> </customer> • • • JAXB Composite Keys/Embedded Key Classes Byte Code Weaving Bidirectional/Cyclical Relationships #DV13 #JPARS JPA @shaunMsmith
  20. 20. Bidirectional Relationship @Entity public class Customer{ ... @OneToMany(mappedBy="owner") private List<Phone> phones; } @Entity public class Phone{ ... @ManyToOne private Customer owner; } #DV13 #JPARS @shaunMsmith
  21. 21. What about @XmlTransient? @Entity public class Customer{ ... @OneToMany(mappedBy=“owner") private List<Phone> phones; } @Entity public class Phone{ ... @ManyToOne @XmlTransient private Customer owner; } #DV13 #JPARS @shaunMsmith
  22. 22. With @XmlTransient Loses the relationship! owner Customer phones #DV13 #JPARS Phone <customer> <phone-numbers> <phone-number> <id>1</id> ... <type>mobile</type> </phone-number> </phone-numbers> </customer> Marshall Marshall X Customer Unmarshall Unmarshall Phone phones @shaunMsmith
  23. 23. EclipseLink XmlInverseReference @Entity public class Customer{ ... @OneToMany(mappedBy=“owner") private List<Phone> phones; } @Entity public class Phone{ ... @ManyToOne @XmlInverseReference(mappedBy=“phones") private Customer owner; } #DV13 #JPARS @shaunMsmith
  24. 24. With @XmlInverseReference EclipseLink restores relationships on unmarshall! owner Customer phones #DV13 #JPARS Phone <customer> <phone-numbers> <phone-number> <id>1</id> ... <type>mobile</type> </phone-number> </phone-numbers> </customer> Marshall Marshall owner Customer Unmarshall Unmarshall Phone phones @shaunMsmith
  25. 25. Demo #DV13 #JPARS @shaunMsmith
  26. 26. Browser JAX-RS JAX-RS Binding Persistence EclipseLink MOXy XML / JSON Java Java Persistence EclipseLink JPA Relational / NoSQL Database #DV13 #JPARS @shaunMsmith
  27. 27. JAX-RS with JPA Example public class InvoiceService {... public Invoice read(int id) { return null; } ... #DV13 #JPARS @shaunMsmith
  28. 28. JAX-RS with JPA Example @Stateless public class InvoiceService {... public Invoice read(int id) { return entityManager.find(Invoice.class, id); } ... #DV13 #JPARS @shaunMsmith
  29. 29. JAX-RS with JPA Example @Path("/invoice") @Stateless public class InvoiceService {... public Invoice read(int id) { return entityManager.find(Invoice.class, id); } ... #DV13 #JPARS http://[machine]:[port]/[web-context]/invoice http://[machine]:[port]/[web-context]/invoice @shaunMsmith
  30. 30. JAX-RS with JPA Example @Path("/invoice") @Stateless public class InvoiceService {... @Path("{id}”) public Invoice read(@PathParam("id") int id) { return entityManager.find(Invoice.class, id); } ... #DV13 #JPARS http://[machine]:[port]/[web-context]/invoice/4 http://[machine]:[port]/[web-context]/invoice/4 @shaunMsmith
  31. 31. JAX-RS with JPA Example @Path("/invoice") @Stateless public class InvoiceService {... @GET @Path("{id}”) public Invoice read(int id) { return entityManager.find(Invoice.class, id); } ... #DV13 #JPARS GET http://[machine]:[port]/[web-context]/invoice/4 GET http://[machine]:[port]/[web-context]/invoice/4 @shaunMsmith
  32. 32. JAX-RS with JPA Example @Path("/invoice") @Stateless public class InvoiceService {... @Produces({"application/xml"}) @GET @Path("{id}") public Invoice read(@PathParam("id") int id) { return entityManager.find(Invoice.class, id); } ... #DV13 #JPARS GET http://[machine]:[port]/[web-context]/invoice/4 GET http://[machine]:[port]/[web-context]/invoice/4 @shaunMsmith
  33. 33. JAX-RS with JPA GET http://.../invoice/4 GET http://.../invoice/4 http://.../invoice/4 mapped to http://.../invoice/4 mapped to JAX-RS JAX-RS beanGET http://.../invoice/4 mapped to method beanGET http://.../invoice/4 mapped to method Invoice Bean GET GET GET GET GET GET GET GET Bean uses JPA Bean uses JPA Contract Bean GET GET GET GET GET GET Payment Bean GET GET GET GET GET GET ... Accounting PU JPA #DV13 #JPARS @shaunMsmith
  34. 34. Browser JAX-RS JAX-RS EclipseLink MOXy EclipseLink MOXy Java Java EclipseLink JPA EclipseLink JPA JPA-RS JPA-RS Database #DV13 #JPARS @shaunMsmith
  35. 35. EclipseLink JPA-RS • Automatically provides REST operations for entities in persistence unit (GET, PUT, POST, DELETE) • Automatic generation of XML and JSON bindings • Leverages EclipseLink’s JAXB/JPA fidelity features to avoid lossy transformations • Automatic publication of REST API metadata #DV13 #JPARS @shaunMsmith
  36. 36. EclipseLink JPA-RS • Supports invocation of named queries via HTTP • Server side caching—EclipseLink clustered cache • Automatic injection of links for entity relationships • Default Resource Model generation #DV13 #JPARS @shaunMsmith
  37. 37. { "id": 2, "city": ”Toronto", … } ... Browser "id": 1, "lastName": "Smith", … "address": { "_link": { "href": "http:/…/Address/2", "method": "GET", "rel": "self" } } { 1 1 2 2 ... Java Database #DV13 #JPARS @shaunMsmith
  38. 38. Configuring JPA-RS • Add JPA-RS web fragment jar to WEB-INF/lib! #DV13 #JPARS @shaunMsmith
  39. 39. JPA-RS GET http://.../persistence/Accounting/Invoice/4 GET http://.../persistence/Accounting/Invoice/4 JAX-RS http://.../persistence/Accounting/Invoice/4 JAX-RS http://.../persistence/Accounting/Invoice/4 JAX-RS JAX-RS mapped to JPA-RS service mapped to JPA-RS service Accounting JPA-RS JPA-RS maps URI JPA-RS maps URI http://.../persistence/Accounting/Invoice/4 to http://.../persistence/Accounting/Invoice/4 to Human Resources Accounting PU and Invoice entity Accounting PU and Invoice Benefits entity ... JPA #DV13 #JPARS @shaunMsmith
  40. 40. Demo #DV13 #JPARS @shaunMsmith
  41. 41. Demo Model #DV13 #JPARS @shaunMsmith
  42. 42. EclipseLink JPA-RS Roadmap • Resource Model • Scope of resource • Entity format • partial entities/projections • Configuration • URL mapping/customization • Manage Entity exposure • Meta-data • JSON Schema? Swagger? ... #DV13 #JPARS @shaunMsmith
  43. 43. Browser JAX-RS EclipseLink MOXy Java EclipseLink JPA Database #DV13 #JPARS @shaunMsmith
  44. 44. Summary • • • Java EE stack has (most of) what you need to build Thin Server Architecture applications You have to be aware of various impedance mismatches EclipseLink JPA-RS • Integrates JAX-RS / JAXB / JPA • Eliminates JAX-RS boilerplate for data access • Simplifies Java EE Thin Server Architecture applications ☛ www.eclipse.org/eclipselink #DV13 #JPARS @shaunMsmith
  45. 45. The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. #DV13 #JPARS @shaunMsmith

×