You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jpacontainer-filtering-criteria-api.asciidoc 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ---
  2. title: Querying with the Criteria API
  3. order: 7
  4. layout: page
  5. ---
  6. [[jpacontainer.filtering.criteria-api]]
  7. = Querying with the Criteria API
  8. When the [interfacename]#Filterable# API is not enough and you need to have more
  9. control, you can make queries directly with the JPA Criteria API. You may also
  10. need to customize sorting or joins, or otherwise modify the query in some way.
  11. To do so, you need to implement a [interfacename]#QueryModifierDelegate# that
  12. the JPAContainer entity provider calls when making a query. The easiest way to
  13. do this is to extend [classname]#DefaultQueryModifierDelegate#, which has empty
  14. implementations of all the methods so that you can only override the ones you
  15. need.
  16. The entity provider calls specific [interfacename]#QueryModifierDelegate#
  17. methods at different stages while making a query. The stages are:
  18. . Start building a query
  19. . Add " [literal]#++ORDER BY++#" expression
  20. . Add " [literal]#++WHERE++#" expression (filter)
  21. . Finish building a query
  22. Methods where you can modify the query are called before and after each stage as
  23. listed in the following table:
  24. [[table.jpacontainer.filtering.criteria-api.methods]]
  25. .[classname]#QueryModifierDelegate# Methods
  26. |===============
  27. |[methodname]#queryWillBeBuilt()#
  28. |[methodname]#orderByWillBeAdded()#
  29. |[methodname]#orderByWasAdded()#
  30. |[methodname]#filtersWillBeAdded()#
  31. |[methodname]#filtersWereAdded()#
  32. |[methodname]#queryHasBeenBuilt()#
  33. |===============
  34. All the methods get two parameters. The [interfacename]#CriteriaBuilder# is a
  35. builder that you can use to build queries. The [interfacename]#CriteriaQuery# is
  36. the query being built.
  37. You can use the [methodname]#getRoots().iterator().next()# in
  38. [interfacename]#CriteriaQuery# to get the "root" that is queried, for example,
  39. the [literal]#++PERSON++# table, etc.
  40. [[jpacontainer.filtering.criteria-api.filters]]
  41. == Filtering the Query
  42. Let us consider a case where we modify the query for a [classname]#Person#
  43. container so that it includes only people over 116. This trivial example is
  44. identical to the one given earlier using the [classname]#Filterable# interface.
  45. ----
  46. persons.getEntityProvider().setQueryModifierDelegate(
  47. new DefaultQueryModifierDelegate () {
  48. @Override
  49. public void filtersWillBeAdded(
  50. CriteriaBuilder criteriaBuilder,
  51. CriteriaQuery<?> query,
  52. List<Predicate> predicates) {
  53. Root<?> fromPerson = query.getRoots().iterator().next();
  54. // Add a "WHERE age > 116" expression
  55. Path<Integer> age = fromPerson.<Integer>get("age");
  56. predicates.add(criteriaBuilder.gt(age, 116));
  57. }
  58. });
  59. ----
  60. See the http://demo.vaadin.com/book-examples-vaadin7/book#jpacontainer.criteria.querymodification[on-line example, window="_blank"].
  61. [[jpacontainer.filtering.criteria-api.compatibility]]
  62. == Compatibility
  63. When building queries, you should consider the capabilities of the different JPA
  64. implementations. Regarding Hibernate, see
  65. <<dummy/../../../framework/jpacontainer/jpacontainer-hibernate#jpacontainer.hibernate.joins,"Joins
  66. in Hibernate vs EclipseLink">>.