Class JpaQueryBuilder


  • public final class JpaQueryBuilder
    extends Object
    Author:
    patrick.deenen, bas.rutten Class for constructing JPA queries built on the criteria API
    • Method Detail

      • createCountQuery

        public static <T> javax.persistence.TypedQuery<Long> createCountQuery​(javax.persistence.EntityManager entityManager,
                                                                              Class<T> entityClass,
                                                                              Filter filter,
                                                                              boolean distinct)
        Creates a query that performs a count
        Parameters:
        entityManager - the entity manager
        entityClass - the entity class
        filter - the filter to apply
        distinct - whether to return only distinct results
        Returns:
      • createDistinctQuery

        public static <T> javax.persistence.TypedQuery<javax.persistence.Tuple> createDistinctQuery​(Filter filter,
                                                                                                    javax.persistence.EntityManager entityManager,
                                                                                                    Class<T> entityClass,
                                                                                                    String distinctField,
                                                                                                    SortOrder... sortOrders)
        Creates a distinct query
        Parameters:
        filter -
        entityManager -
        entityClass -
        distinctField -
        sortOrders -
        Returns:
      • createFetchQuery

        public static <ID,​T> javax.persistence.TypedQuery<T> createFetchQuery​(javax.persistence.EntityManager entityManager,
                                                                                    Class<T> entityClass,
                                                                                    List<ID> ids,
                                                                                    Filter additionalFilter,
                                                                                    SortOrders sortOrders,
                                                                                    FetchJoinInformation[] fetchJoins)
        Creates a query that fetches objects based on their IDs
        Parameters:
        entityManager - the entity manager
        entityClass - the entity class
        ids - the IDs of the desired entities
        sortOrders - the sorting information
        fetchJoins - the desired fetch joins
        Returns:
      • createFetchSingleObjectQuery

        public static <ID,​T> javax.persistence.TypedQuery<T> createFetchSingleObjectQuery​(javax.persistence.EntityManager entityManager,
                                                                                                Class<T> entityClass,
                                                                                                ID id,
                                                                                                FetchJoinInformation[] fetchJoins)
        Create a query for fetching a single object
        Parameters:
        entityManager - the entity manager
        entityClass - the entity class
        id - ID of the object to return
        fetchJoins - fetch joins to include
        Returns:
      • createIdQuery

        public static <T> javax.persistence.TypedQuery<javax.persistence.Tuple> createIdQuery​(javax.persistence.EntityManager entityManager,
                                                                                              Class<T> entityClass,
                                                                                              Filter filter,
                                                                                              SortOrder... sortOrders)
        Creates a query for retrieving the IDs of the entities that match the provided filter
        Parameters:
        entityManager - the entity manager
        entityClass - the entity class
        filter - the filter to apply
        sortOrder - the sorting to apply
        Returns:
      • createParameterMap

        public static Map<String,​Object> createParameterMap()
        Creates an empty parameter map
        Returns:
      • createSelectQuery

        public static <T> javax.persistence.TypedQuery<T> createSelectQuery​(Filter filter,
                                                                            javax.persistence.EntityManager entityManager,
                                                                            Class<T> entityClass,
                                                                            FetchJoinInformation[] fetchJoins,
                                                                            SortOrder... sortOrders)
        Creates a query that simply selects some objects based on some filter
        Parameters:
        filter - the filter
        entityManager - the entity manager
        entityClass - the entity class
        sortOrder - the sorting information
        Returns:
      • createSelectQuery

        public static <T> javax.persistence.TypedQuery<Object[]> createSelectQuery​(Filter filter,
                                                                                   javax.persistence.EntityManager entityManager,
                                                                                   Class<T> entityClass,
                                                                                   String[] selectProperties,
                                                                                   SortOrders sortOrders)
        Creates a query that fetches properties instead of entities. Supports aggregated functions; when used will automatically add group by expressions for all properties in the select list without an aggregated function.
        Parameters:
        filter - the filter
        entityManager - the entity manager
        entityClass - the entity class
        selectProperties - the properties to use in the selection
        sortOrders - the sorting information
        Returns:
      • createUniquePropertyFetchQuery

        public static <T> javax.persistence.criteria.CriteriaQuery<T> createUniquePropertyFetchQuery​(javax.persistence.EntityManager entityManager,
                                                                                                     Class<T> entityClass,
                                                                                                     FetchJoinInformation[] fetchJoins,
                                                                                                     String propertyName,
                                                                                                     Object value,
                                                                                                     boolean caseSensitive)
        Creates a query to fetch an object based on a value of a unique property
        Parameters:
        entityManager - the entity manager
        entityClass - the entity class
        fetchJoins - the fetch joins to include
        propertyName - name of the property to search on
        value - value of the property to search on
        Returns:
      • createUniquePropertyQuery

        public static <T> javax.persistence.criteria.CriteriaQuery<T> createUniquePropertyQuery​(javax.persistence.EntityManager entityManager,
                                                                                                Class<T> entityClass,
                                                                                                String propertyName,
                                                                                                Object value,
                                                                                                boolean caseSensitive)
        Creates a query used to retrieve a single entity based on a unique property value
        Parameters:
        entityManager -
        entityClass -
        propertyName -
        value -
        Returns:
      • setParameters

        public static void setParameters​(javax.persistence.TypedQuery<?> query,
                                         Map<String,​Object> pars)
        Sets any parameter values on the query
        Parameters:
        query - the query
        pars - the parameter values