Class CriteriaQueryBuilder<T>


  • public class CriteriaQueryBuilder<T>
    extends java.lang.Object
    An extension of CriteriaQuery that uses a Builder-style pattern when adding "WHERE" and/or "ORDER BY" clauses.

    CriteriaQuery, as is, requires that all clauses must be passed in at once -- if one calls "WHERE" multiple times, the later call overwrites the earlier call.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  CriteriaQueryBuilder.WhereOperator<U>
      Functional interface that defines the 'where' operator, e.g.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      javax.persistence.criteria.CriteriaQuery<T> build()
      Builds and returns the query, applying all WHERE and ORDER BY clauses at once.
      static <T> CriteriaQueryBuilder<T> create​(java.lang.Class<T> clazz)
      Creates a query builder that will SELECT from the given class.
      static <T> CriteriaQueryBuilder<T> create​(javax.persistence.EntityManager em, java.lang.Class<T> clazz)
      Creates a query builder for the given entity manager.
      static <T> CriteriaQueryBuilder<java.lang.Long> createCount​(javax.persistence.EntityManager em, java.lang.Class<T> clazz)
      Creates a "count" query for the table for the class.
      CriteriaQueryBuilder<T> orderByAsc​(java.lang.String fieldName)
      Orders the result by the given field ascending.
      CriteriaQueryBuilder<T> orderByDesc​(java.lang.String fieldName)
      Orders the result by the given field descending.
      <V> CriteriaQueryBuilder<T> where​(java.lang.String fieldName, CriteriaQueryBuilder.WhereOperator<V> whereClause, V value)
      Adds a WHERE clause to the query, given the specified operation, field, and value.
      <V> CriteriaQueryBuilder<T> whereFieldContains​(java.lang.String fieldName, java.lang.Object value)
      Adds a WHERE clause to the query specifying that a collection field must contain a particular value.
      CriteriaQueryBuilder<T> whereFieldIsIn​(java.lang.String fieldName, java.util.Collection<?> values)
      Adds a WHERE clause to the query specifying that a value must be in the given collection.
      CriteriaQueryBuilder<T> whereFieldIsNotIn​(java.lang.String fieldName, java.util.Collection<?> values)
      Adds a WHERE clause to the query specifying that a value must not be in the given collection.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • whereFieldIsIn

        public CriteriaQueryBuilder<T> whereFieldIsIn​(java.lang.String fieldName,
                                                      java.util.Collection<?> values)
        Adds a WHERE clause to the query specifying that a value must be in the given collection.
      • whereFieldIsNotIn

        public CriteriaQueryBuilder<T> whereFieldIsNotIn​(java.lang.String fieldName,
                                                         java.util.Collection<?> values)
        Adds a WHERE clause to the query specifying that a value must not be in the given collection.
      • whereFieldContains

        public <V> CriteriaQueryBuilder<T> whereFieldContains​(java.lang.String fieldName,
                                                              java.lang.Object value)
        Adds a WHERE clause to the query specifying that a collection field must contain a particular value.
      • orderByAsc

        public CriteriaQueryBuilder<T> orderByAsc​(java.lang.String fieldName)
        Orders the result by the given field ascending.
      • orderByDesc

        public CriteriaQueryBuilder<T> orderByDesc​(java.lang.String fieldName)
        Orders the result by the given field descending.
      • build

        public javax.persistence.criteria.CriteriaQuery<T> build()
        Builds and returns the query, applying all WHERE and ORDER BY clauses at once.
      • create

        public static <T> CriteriaQueryBuilder<T> create​(java.lang.Class<T> clazz)
        Creates a query builder that will SELECT from the given class.
      • create

        public static <T> CriteriaQueryBuilder<T> create​(javax.persistence.EntityManager em,
                                                         java.lang.Class<T> clazz)
        Creates a query builder for the given entity manager.
      • createCount

        public static <T> CriteriaQueryBuilder<java.lang.Long> createCount​(javax.persistence.EntityManager em,
                                                                           java.lang.Class<T> clazz)
        Creates a "count" query for the table for the class.