JEST: REST on OpenJPA

  • 2,058 views
Uploaded on

JEST unifies REST and JPA for language neutral clients running in web browser or smart phone

JEST unifies REST and JPA for language neutral clients running in web browser or smart phone

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,058
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. JEST: REST on OpenJPA Pinaki Poddar, PhD Java Persistence Evangelist [email_address]
  • 2. Agenda Introduction to JEST Integration of REST and OpenJPA Resource Representation URI Syntax Transaction JEST in Action Conclusion Q&A
  • 3. JEST: REST on OpenJPA JEST is RESTful interaction with OpenJPA Runtime http JDBC XML/JSON Java Object Graph
  • 4. JEST: REST on OpenJPA Deploy-and-Play No annotation on persistent classes No browser plug-in No client knowledge of persistent schema KISS: Keep It Short and Simple
  • 5. JEST: Primary Use Cases Access persistent object graph by language-neutral, domain-agnostic clients or Web Services Visualize OpenJPA runtime internals via Web Browser-based Admin console Transact with any OpenJPA application from language-neutral clients without violating REST principles
  • 6. REST: RE presentational S tate T ransfer C0 C0 C1’ C1’ S1 S2 S1 renders C1 modifies C2 renders Response 1 Request 2 Request 1 Response 2 @ rest State Transition 1 2 3 4 5 6
  • 7. REST Principles Identifiable Resource ( URI ) based, not API-based Document-oriented Resource Representation XML, JSON, … Stateless Server Standard Communication Protocol HTTP
  • 8. Java Persistence API Persistence framework for Java Objects mapped to Relational database EntityManagerFactory EntityManager Persistence Unit Persistence Context Second-Tier Caches (optional) Relational Database User Application Data Cache META-INF/persistence.xml Java Object Graph
  • 9. JPA is Model-View-Controller for Persistence View Control Model /** * A persistent entity **/ @Entity public class Actor { @Id private String name ; } /** * Find Actor by name **/ public Actor findByName( String name ) { EntityManager em = …; return em.find( Actor . class , name); } POJO sees uses updates modifies Java Persistence API Relational Database
  • 10. JPA: access-detach-merge programming model C0 C0 C1’ C1’ em1 em2 renders C1 modifies C2 renders State Transition SELECT … detach find merge commit UPDATE/INSERT/DELETE … 1 2 3 5 6 4
  • 11. REST and JPA: brothers separated at birth? Stateless Server Detached Transaction Identifiable Resource Persistent Identity Coherent Representation Persistent Closure Self-descriptive JPA 2.0 Metamodel REST JPA
  • 12. JEST Resource Perisistent Object Graph Customizable Coherent
  • 13. Customizable, Persistent Closure A persistence context as a graph managed entity as vertex persistent relation as edge Persistent Closure of an entity x is defined as set of entities C(x)={e} such that e is reachable from x in graph-theoretic sense. Every entity is reachable to itself, by definition . Customization is ability to define what is reachable at runtime. x a c1 b c2 d C(x) = {x, a, b, c1, c2, d} C(x) = {x, a, b, d} could be customized to and its complete closure C(x) = {x, b} or An object graph…
  • 14. Identifiable Resource: URI for persistent object? Actor p1 = em1.find ( Actor . class , “ John ”); Actor p2 = em2.find ( Actor . class , “ John ”); name :” John ” hash : 56789 p1 name :” John ” hash : 56789 p2 p1.equals(p2) p1.hashCode() == p2.hashCode() p1 != p2 public class Actor { @Id String name ; } Persistent Identity carries the notion that both p1 and p2 refer to the same database record. EntityManager EntityManager … Robert … John ACTOR
  • 15. Customizable, Persistent Closure: State of the art JEST response contains customizable, persistent closure JPA provides preliminary semantics on customized closure, statically OpenJPA FetchPlan already provides rich syntax and semantics to customize closure, dynamically . Future JPA specification will enhance support on this area. This feature had been the most sought after user requirement during JavaOne 2010.
  • 16. OpenJPA Fetch Plan @Entity @FetchGroups ({ @FetchGroup ( name =" OnlyName ", attributes ={ @FetchAttribute ( name =" firstName "), @FetchAttribute ( name =" lastName ") }) }) public class Actor { @Id private String id ; private String firstName ; private String lastName ; private Gender gender ; private Date dob ; @OneToOne private Actor partner ; @OneToMany private Set<Movie> movies ; // getters and setters }
  • 17. JEST Representation Theme Meta-meta-data driven as opposed to Domain-driven Content Customizable closure of persistent object graph Reference Resolution Coherent, not Chatty Format XML and JSON Thrift, Protobuf, Avro … (not right now, but tomorrow)
  • 18. JPA Metamodel: Persistent Type System Class Type<X> ManagedType<X> BasicType<X> IdentifiableType<X> EmbeddableType<X> EntityType<X> MappedSuperclassType<X> package javax.persistence.metamodel package java.lang <X>: Type<X> represents Java class X
  • 19. JPA Metamodel : Persistent Type System Field Attribute<X,Y> SingularAttribute<X,Y> PluralAttribute<X,C,E> ListAttribute<X,E> CollectionAttribute<X,E> SetAttribute<X> MapAttribute<X,K,V> package javax.persistence.metamodel package java.lang.reflect <X>: Attribute<X,Y> declared in Java class X <Y>: Attribute<X,Y> is of type Y <C>: PluralAttribute<X,C,E> is of Java collection type C <E>: PluralAttribute<X,C,E> contains elements of type E
  • 20. JPA Metamodel: Persistent Scope java.lang.Class java.lang.reflect.Field Metamodel ManagedType<X> Attribute<X,Y> java.lang.ClassLoader A classloader defines a scope for a set of Java classes A metamodel defines a scope for a set of managed persistent types package javax.persistence.metamodel
  • 21. Meta-Meta-Data Driven Representation < Actor > < id > 1234 </ id > < firstName > John </ firstName > < lastName > Doe </ lastName > </ Actor > Domain Driven Representation Domain-variant Schema < instance id = “Actor-1234” type =“Actor” > < id name = “ id ” type = “long” > 1234 </ id > < basic name = “firstName” type = “String” > John </ basic > < basic name = “lastName” type = “String” > Doe </ basic > </ instance > Model Driven Representation Domain-invariant Schema JPA 2.0 MetaModel based XML Schema compliant jest-instances.xsd Common Domain-based schema some-domain.xsd
  • 22. JSON-based Representation in JEST JSON does not support cycles in graphs JEST supports cyclic object graph [ { "$id" : Actor-m2 , "id": "m2", "dob": "Tue May 14 1940", "firstName": "Robert", "lastName": "De Niro", "gender": "Male", "partner": { "$id" : Actor-f3 , "id": "f3", "dob": “Wed May 15 1960", "firstName": "Jodie", "lastName": "Foster", "gender": "Female“, “ partner”: { “ $ref” : Actor-m2 } } } ] dummy $id field dummy $ref field resolved circular reference JSON by JEST
  • 23. JEST URI Syntax identifies JPA resources. maps to JPA operation decorates JPA operation JEST resolves argument strings to strong types, e.g. “ type=Actor ” to Actor .class . scheme authority path query context action qualifiers arguments OpenJPAEntityManager em = getPersistenceContext(); em.addFetchPlan(“ basic ”); Actor a = em.find( Actor . class , “m1”); find plan=basic type=Actor ? jest / / & id=m1 http 8080 openjpa.com / : ://
  • 24. JEST URI Examples find an Actor with primary key m1 using fetch plan ‘basic’ http :// openjpa.com:8080 / jest / find / plan = basic ? type = Actor & m1 Query for a single Actor by name John http :// openjpa.com:8080 / jest / query / single ? q = select p from Actor p where p.name=:n & n = John Get the metamodel http :// openjpa.com:8080 / jest / domain
  • 25. JEST translates HTTP verbs to JPA operations GET HTTP POST PUT DELETE find() getMetamodel() createQuery().getResultList() getProperties() /find / query / domain / properties merge() persist() remove() JEST Action JPA Operation non-transactional transactional
  • 26. JEST Deployment < web-app > < servlet > < servlet-name > demo </ servlet-name > < servlet-class > demo.SimpleApp </ servlet-class > </ servlet > < servlet-mapping > < servlet-name > demo </ servlet-name > < url-pattern > /* </ url-pattern > </ servlet-mapping > <!-- Deployment descriptor for JESTServlet. --> < servlet > < servlet-name > jest </ servlet-name > < servlet-class > org.apache.openjpa.persistence.jest.JESTServlet </ servlet-class > < init-param > < param-name > persistence.unit </ param-name > < param-value > SimplePU </ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name > jest </ servlet-name > < url-pattern > /jest/* </ url-pattern > </ servlet-mapping > </ web-app > Deployed as HTTP Servlet within the same module scope Identifies the module by persistence unit name
  • 27. Deployment Modes for JEST Servlet JESTServlet emf Module X emf JESTServlet instantiates discovers instantiates or injected primary mode auxiliary mode default
  • 28. JEST Client: An afterthought JEST works from navigation bar of a web browser A JEST client is developed as an afterthought for feature demonstration uses dojo JavaScript library Why dojo? everybody likes cool widgets validates JEST representation for XML/JSON with dojo parsers
  • 29. Why JEST? Managed, persistent object graphs can not be effectively represented as REST resources without JPA knowledge JPA Metamodel is the right basis for self-describing REST representation Detached transaction support of JPA is the right model for stateless REST style server Internal states of OpenJPA runtime are useful information for administration console
  • 30. JEST: JPA Detached Transaction C1 C1 C1’ C1’ S1 S2 S1 renders C1 modifies C2 renders A1 A1’ R1 Response 2 @ rest State Transition find() detach() represent() parse() merge() commit()
  • 31. REST is successful in real world Success has many friends… “ Amazon has both SOAP and REST interfaces to their web services, and 85% of their usage is of the REST interface. ” Jeff Barr, Web Services Evangelist @ Amazon Reference: REST vs SOAP at Amazon
  • 32. Does JEST pass the RESTness est? YES Q5 . Can server transfer logic to clients? NO Q4 . Can client determine the exact server endpoint? YES Q3 . Does response describe their cacheability for the client? NO Q2 . Does server store client context between requests? YES Q1 . Are clients separated from servers by a uniform interface?
  • 33. Does JEST pass Uniform Interface Test? YES Q9 . Are related resources accessible via received representation? YES Q8 . Does representation carry enough information to be processed by the client? YES Q7 . Can client manipulate received resource representation? YES Q6 . Does request identify a resource?
  • 34. Why JEST? JEST explores naturalness between REST and JPA for language-neutral, schema-agnostic clients to access and transact with OpenJPA based applications JEST solves representational issues for managed, persistent object graph using Metamodel Persistent Closure Fetch Plan
  • 35. How JEST can help your effort? Similar work such as JAX-RS will benefit from model-driven, coherent representation in JEST in XML and JSON JEST and Fetch Plan will obviate extra annotations in persistent domain classes Dynamic customization of object graph is a powerful, distinctive feature of OpenJPA Explore JEST to your advantage.
  • 36. Q&A In case, you are in SF on Feb 2011: http://www.sfjava.org/calendar/15592287/ JEST Documentation https://cwiki.apache.org/openjpa/jest.html