Thursday, 4 April 2013

Hacking on JPA Criteria


[1] CriteriaBuilder
CriteriaBuilder interface is the factory for CriteriaQuery. A CriteriaBuilder is obtained from either an EntityManagerFactory or an EntityManager.


EntityManager em = ... ; 
CriteriaBuilder queryBuilder = em.getCriteriaBuilder();
CriteriaQuery criteriaQuery = queryBuilder.createQuery();



[2] CriteriaQuery

Root<User> user = criteriaQuery.from(User.class);




[3] Root
Query roots specify the domain objects on which the query is evaluated. Query root is an instance of the Root<T> interface.

Root<History> histories = user.join(user.get(User_.histories));

criteriaQuery.where(user.get(User_.userName).equal("prayag@zazzercode.com")
              .and(histories.get(History_.historyType).equal(HistoryType.ADD).not()));

criteriaQuery.select(user.get(User_.userName), histories.get(Hostory_.historyType));

The attribute "userName" refers to a valid persistent property of User(the receiving domain object).




[4] Predicate



References
Chapter 11.  JPA Criteria,
http://openjpa.apache.org/builds/2.1.1/apache-openjpa/docs/jpa_overview_criteria.html

Chapter 3.  Java Persistence API Architecture,
http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/jpa_overview_arch.html

Chapter 15. Criteria Queries,
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html

Crash Introduction to Modern Java Data Access: Understanding JPA, Hibernate, MyBatis, and pureQuery, http://www.slideshare.net/OnBuildingSoftware/crash-introduction-to-modern-java-data-access-understanding-jpa-hibernate-mybatis-and-purequery

http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html

No comments:

Post a Comment