160 likes | 261 Views
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.
E N D
In LactureCSS-441Advanced 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
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
HQL ● Powerful object-based query language ● Hibernate translates HQL to SQL ● HQL statements are shorter, more • readable than their SQL counterparts
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);
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.
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();
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.
How to use the JPA Critiera API • CriteriaBuilder cb = em.getCriteriaBuilder(); • CriteriaQuery cqry = em.createQuery();
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
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
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
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
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
Reference • http://www.oracle.com • http://www.jumpingbean.co.za/blogs/jpa2-criteria-api