3. JEST: REST on OpenJPA JEST is RESTful interaction with OpenJPA Runtime http JDBC XML/JSON Java Object Graph
4.
5.
6.
7. 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
8.
9.
10. 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
12. 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
13.
14.
15. 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
16.
17. 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 }
18.
19. 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
20. 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
21. 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
22.
23.
24. 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 / : ://
25. 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
26. 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
37. 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
38. 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?
39. 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?