Java persistence api ppt

Java Persistence Query Language (JPQL) and java persistence with hibernate ppt
LexiWills Profile Pic
LexiWills,United Kingdom,Professional
Published Date:01-08-2017
Your Website URL(Optional)
Comment
Java Persistence Criteria API Linda DeMichiel OracleAGENDA Background: Where we started Criteria API objects and interfaces Examples and issues JPA Metamodel and metamodel objects CriteriaQuery definition CriteriaQuery execution Summary and what’s next 2 Background: Where We Started Java Persistence Query Language (JPQL) String-based query language SQL-like syntax – Operates over “abstract schema” of persistence unit – Path navigation syntax for relationships and state traversal – Translated by provider into native SQL Static queries – Metadata (using NamedQuery annotations and/or XML) – In code Dynamic queries – Runtime string construction 3 Background: Where We Started Java Persistence Query Language (JPQL) JPA 2.0 provides some much-needed improvements – Operations in SELECT list – Non-polymorphic queries – CASE statements – Collection-valued IN parameters – Date / time / timestamp literals – Operations over new features (ordered lists, maps, etc.) Syntax extended by vendors 4 Background: Where We Started Java Persistence Query Language (JPQL) Positives – Easy to learn SQL-like string-based QL – Metadata-based queries provide for precompilation and/or deployment-time configurability – Dynamic query construction via straightforward string manipulation Negatives – Strings – No type-safety – Usual security issues with SQL string construction – Pre-runtime semantic checks for metadata-based queries only 5 Criteria API Addresses requests from community for object-based query API Several such APIs already available in community when we started – Hibernate Criteria API, TopLink Expression API, Cayenne, OJB, … Design went through MANY variations and much evolution in Expert Group Two main design principles emerged – “JPQL-completeness” – Type safety We heard from many people working on projects addressing similar goals – Squill, Querydsl, JaQu, LIQUidForm, … 6 Criteria API Object-based API Strongly typed – Heavy use of Java generics – Based on type-safe metamodel of persistence unit – Typing carries through to query execution Designed to mirror JPQL semantics – Objects / methods mirror JPQL / SQL syntactic and semantic constructs Supports object-based or string-based navigation 7 Criteria Query Objects CriteriaQuery objects are composites Component objects include: – Roots – Join objects – Expressions – Predicates – Selections – Orderings – Groupings – … Some of these are composites as well 8 Key Interfaces: CriteriaBuilder Factory for CriteriaQuery objects Factory for Expression objects, including Predicate objects Methods for – comparisons – operations over strings, numbers, booleans, etc – operations over collections – operations over subqueries, … – CASE and IN-expression builders – creating parameters – … Obtained from EntityManager or EntityManagerFactory – getCriteriaBuilder() method 9 Example: CriteriaBuilder Methods E, C extends CollectionE Predicate isMember( E elem, ExpressionC collection); E, C extends CollectionE Predicate isMember( ExpressionE elem, ExpressionC collection); ExpressionString concat(ExpressionString x, ExpressionString y); ExpressionString concat(ExpressionString x, String y); ExpressionString concat(String x, ExpressionString y); Y ExpressionY all(SubqueryY subquery); 10 Key Interfaces: CriteriaQuery CriteriaQuery object represents a query definition – Can be constructed incrementally and/or reconstructed – Can be browsed; constituent objects can be extracted Methods to assign / replace constituent objects select(), multiselect() from() where() orderBy() groupBy(), having() Methods to browse CriteriaQuery objects getSelection(), getRoots(), getRestriction(), getOrderList(), getGroupList(), getGroupRestriction() Obtained from CriteriaBuilder – createQuery(), createTupleQuery() 11 Interfaces that Model Query Components Root – Query roots Join – Joins from a root or existing join – CollectionJoin, SetJoin, ListJoin, MapJoin subinterfaces Path – Navigation from a root, join, or path Subquery Selection, CompoundSelection Expression Predicate … 12 Example: The World’s Simplest Query JPQL: SELECT c FROM Customer c Criteria API: EntityManager em = … CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQueryCustomer cq = cb.createQuery(Customer.class); RootCustomer customer = cq.from(Customer.class); cq.select(customer); TypedQueryCustomer tq = em.createQuery(cq); ListCustomer resultList = tq.getResultList(); 13 Example (with Join) SELECT c FROM Customer c join c.orders o EntityManager em = … CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQueryCustomer cq = cb.createQuery(Customer.class); RootCustomer customer = cq.from(Customer.class); JoinCustomer, Order o = customer.join(“orders”); cq.select(customer); 14 Example (with Restriction) SELECT c FROM Customer c join c.orders o WHERE c.name = ‘Braun’ CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQueryCustomer cq = cb.createQuery(Customer.class); RootCustomer customer = cq.from(Customer.class); JoinCustomer, Order o = customer.join(“orders”); cq.where(cb.equal(customer.get(“name”), “Braun”)) .select(customer); 15 What’s the Problem? SELECT c FROM Customer c join c.orders o WHERE c.name = ‘Braun’ CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQueryCustomer cq = cb.createQuery(Customer.class); RootCustomer customer = cq.from(Customer.class); JoinCustomer, Order o = customer.join(“order”); cq.where(cb.equal(customer.get(“names”), “Braun”)) .select(customer); 16 The Issue: A Hole in the Typing How to specify navigation in join() and get() methods – Strings vs objects With strings: – No guarantee referenced objects exist – Type safety falls apart But: – Strings are easier to use + familiar + intuitive Source of MUCH discussion in JPA Expert Group – Some developers may prefer string-based navigation, so we need to support it – However: not the design-center of the API 17 What’s the Solution to Type-safe Navigation? Class literals? (Customer.class, etc.) – Used in creating CriteriaQuery objects, Root objects, TypedQuery objects, … – Don’t work for specifying joins and paths Member literals? – Oops 18 Metamodel View over the “abstract schema” of the persistence unit – Entities, embeddables, mapped superclasses, and their attributes Accessed at runtime – EntityManager.getMetamodel() – EntityManagerFactory.getMetamodel() Useful for frameworks – Can dynamically discover all the managed types, their attributes, and their relationships Doesn’t yet cover O / R mapping level – Potential candidate for future release 19 Metamodel Interfaces: Types TypeX ManagedTypeX BasicTypeX EmbeddableTypeX IdentifiableTypeX MappedSuperclassTypeX EntityTypeX 20

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.