16
Topic : JPA Criteria API Kaster Nurmukan

In Lacture CSS-441 Advanced Programming using JAVA EE

  • Upload
    duff

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: In Lacture CSS-441 Advanced Programming using JAVA EE

Topic : JPA Criteria APIKaster Nurmukan

Page 2: In Lacture CSS-441 Advanced Programming using JAVA EE

• 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

Page 3: In Lacture CSS-441 Advanced Programming using JAVA EE

3

● 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

Page 4: In Lacture CSS-441 Advanced Programming using JAVA EE

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

4

Page 5: In Lacture CSS-441 Advanced Programming using JAVA EE

• 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);

Page 6: In Lacture CSS-441 Advanced Programming using JAVA EE

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

Page 7: In Lacture CSS-441 Advanced Programming using JAVA EE

• 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();

Page 8: In Lacture CSS-441 Advanced Programming using JAVA EE

• JQL seems like a great way to leverage your existing SQL JQL seems like a great way to leverage your existing SQL knowledgeknowledge

• no compile time checkingno compile time checking• JPA Criteria API quiet a productivity drain with developers quiet a productivity drain with developers

having to correcthaving to correct, compile and redeploy to  continue.

Page 9: In Lacture CSS-441 Advanced Programming using JAVA EE

• CriteriaBuilder cb = em.getCriteriaBuilder();• CriteriaQuery cqry = em.createQuery();

Page 10: In Lacture CSS-441 Advanced Programming using JAVA EE

 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

Page 11: In Lacture CSS-441 Advanced Programming using JAVA EE

•  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

Page 12: In Lacture CSS-441 Advanced Programming using JAVA EE

•              //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

Page 13: In Lacture CSS-441 Advanced Programming using JAVA EE

•              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

Page 14: In Lacture CSS-441 Advanced Programming using JAVA EE

• 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

Page 15: In Lacture CSS-441 Advanced Programming using JAVA EE

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

Page 16: In Lacture CSS-441 Advanced Programming using JAVA EE