1 / 16

In Lacture CSS-441 Advanced Programming using JAVA EE

In Lacture CSS-441 Advanced Programming using JAVA EE. Topic : JPA Criteria API Kaster Nurmukan. Agenda. Powerful Query Capabilities HQL Prototypical Use of Query API Criteria Queries Prototypical Use of Criteria API How to use the JPA Criteria API Examples from simple to complex.

duff
Download Presentation

In Lacture CSS-441 Advanced Programming using JAVA EE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. In LactureCSS-441Advanced Programming using JAVA EE Topic : JPA Criteria API Kaster Nurmukan

  2. Agenda Powerful Query Capabilities HQL Prototypical Use of Query API Criteria Queries Prototypical Use of Criteria API How to use the JPA Criteria API Examples from simple to complex

  3. Powerful Query Capabilities ● HQL: The Hibernate Query Language ● object-oriented ● Criteria API ● powerful object model for constructing and executing queries ● Query by Example ● Not locked in: can perform SQL queries, including stored procedure invocations

  4. HQL ● Powerful object-based query language ● Hibernate translates HQL to SQL ● HQL statements are shorter, more • readable than their SQL counterparts

  5. Prototypical Use of Query API • String hql = "from Customer c where c.age > :age"; • Query q = session.createQuery(); • q.setInteger("age", 33); • q.setFirstResult(20); • q.setMaxResults(10);  // fetch the third page • List customers = q.list(hql);

  6. Criteria Queries • ● What makes the Criteria API powerful is • that it allows queries to be specified by • composition. • ● This means that queries can be • constructed dynamically.

  7. Prototypical Use of Criteria API • Criteria c = session.createCriteria(Customer.class); • c.add( Restrictions.ilike("name", "Albert%") ); • c.addOrder( Order.asc("age") ); • c.setMaxResults(20); • c.list(); • // entire sequence of calls can also be chained,  • // like so: • session.createCriteria(Customer.class). •   add( Restrictions.ilike("name", "Albert%") ). •   addOrder( Order.asc("age") ). •   setMaxResults(20). •   list();

  8. The New Problem JPQL can’t,But JPA Criteria API can? • JQL seems like a great way to leverage your existing SQL knowledge • no compile time checking • JPA Criteria API quiet a productivity drain with developers having to correct, compile and redeploy to  continue.

  9. How to use the JPA Critiera API • CriteriaBuilder cb = em.getCriteriaBuilder(); • CriteriaQuery cqry = em.createQuery();

  10. A Simple CriteriaQuery Example  CriteriaBuilder cb = em.getCriteriaBuilder(); //Step 1  CriteriaQuery cqry = em.createQuery(); //Step 1 •   //Interesting stuff happens here •  Root<MyEntity> root = cqry.from(MyEntity.class); //Step 2 •              cqry.select(root); Step 3 •              …. •  Query qry = em.createQuery(cqry); //Step 6  List<MyEnity> results = qry.getResultList(); //Step 6

  11. Example continue… •  Root<MyEntity> root = cqry.from(MyEntity.class); //Step 2 •    cqry.select(root); //Step 3 •  Predicate pGtAge = cb.gt(root.get("age"),10); //Step 4 •      cqry.where(pGtAge); //Step 5  Root<MyEnity> root = cqry.from(MyEntity.class); //Step 2 •      cqry.select(root); //Step 3 •      Predicate pGtAge = cb.gt(root.get("age"),10); //Step 4 •      Predicate pGtDateCreated= •   cb.greaterThan(root.get("dateCreated"),date); //Step 4 Predicate pAnd = cb.and(pGtDateCreated,pGtAge); //Step 4 •     cqry.where(pAnd); //Step 5

  12. Using Meta Model Classes in Query •              //assume we have created a date object for 2011-07-01  •             //called date •              Root<MyEnity> root = cqry.from(MyEntity.class); //Step 2 •              cqry.select(root); //Step 3 •              Predicate pGtAge = cb.gt(root.get(MyEntity_.age),10); //Step 4 •              Predicate pGtDateCreated= •                cb.greaterThan(root.get(MyEntity_.dateCreated),date); //Step 4 •              Predicate pAnd = cb.and(pGtDateCreated,pGtAge); //Step 4 •              cqry.where(pAnd); //Step 5

  13. Criteria Query Using Joins •              Root<MyEnity> root = cqry.from(MyEntity.class); //Step 2 •              Join<MyEntity,AnotherEntity> join = •                              root.join(MyEntity_.anotherEntity); //Step 2 •             //Join<MyEntity,AnotherEntity> join = •                              root.join("anotherEntity"); //Step 2 •              cqry.select(root); //Step 3 •              Predicate pGtAge = cb.gt(root.get(MyEntity_.age),10); //Step 4 •              Predicate pGtDateCreated= •                              cb.greaterThan(root.get(MyEntity_.dateCreated),date); //Step 4 •              Predicate pEqEnabled = cb.equals(join.get(AnotherEntity_.enabled),false); •              Predicate pAnd = cb.and(pGtDateCreated,pGtAge,pEqEnabled); //Step 4 •              cqry.where(pAnd); //Step 5

  14. More Complex Select Clause • Root<MyEnity> root = cqry.from(MyEntity.class); //Step 2 •              cqry.select(root.get(MyEntity_.dateCreated)); //Step 3 • If we wanted to use an aggregate function and get the minimum dateCreated we could use something like: •              Root<MyEnity> root = cqry.from(MyEntity.class); //Step 2 •              Expression min = •                       cb.min(root.get(MyEntity_.dateCreated));//Step3 •              cqry.select(min); //Step 3

  15. Reference • http://www.oracle.com • http://www.jumpingbean.co.za/blogs/jpa2-criteria-api

  16. Q&A

More Related