In Lacture CSS-441 Advanced Programming using JAVA EE
description
Transcript of In Lacture CSS-441 Advanced Programming using JAVA EE
Topic : JPA Criteria APIKaster Nurmukan
• 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
● 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
● Powerful object-based query language● Hibernate translates HQL to SQL● HQL statements are shorter, more • readable than their SQL counterparts
4
• 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);
• ● What makes the Criteria API powerful is• that it allows queries to be specified by • composition.• ● This means that queries can be • constructed dynamically.
• 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();
• 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.
• CriteriaBuilder cb = em.getCriteriaBuilder();• CriteriaQuery cqry = em.createQuery();
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
• 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
• //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
• 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
• 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
• http://www.oracle.com• http://www.jumpingbean.co.za/blogs/jpa2-criteria-api