Class QueryUtils


  • public class QueryUtils
    extends Object
    Utilities for Query.
    Author:
    poirigui
    • Field Detail

      • MAX_PARAMETER_LIST_SIZE

        public static final int MAX_PARAMETER_LIST_SIZE
        Largest parameter list size for which optimizeParameterList(Collection) should be used. Past this size, no padding will be performed and a warning will be emitted.
        See Also:
        Constant Field Values
    • Constructor Detail

      • QueryUtils

        public QueryUtils()
    • Method Detail

      • optimizeParameterList

        public static <T extends Comparable<T>> Collection<T> optimizeParameterList​(Collection<T> list)
        Optimize a given parameter list by sorting, removing duplicates and padding to the next power of two.

        This is a temporary solution until we update to Hibernate 5.2.18 which introduced hibernate.query.in_clause_parameter_padding. Read more about this topic.

      • batchParameterList

        public static <T extends Comparable<T>> List<List<T>> batchParameterList​(Collection<T> list,
                                                                                 int batchSize)
        Partition a parameter list into a collection of batches of a given size.

        It is recommended to use a power of two in case the same query is also prepared via optimizeParameterList(Collection). This will make it so that the execution plan can be reused.

      • batchIdentifiableParameterList

        public static <T extends IdentifiableList<List<T>> batchIdentifiableParameterList​(Collection<T> list,
                                                                                            int batchSize)
      • listByBatch

        public static <S extends Comparable<S>,​T> List<T> listByBatch​(Query query,
                                                                            String batchParam,
                                                                            Collection<S> list,
                                                                            int batchSize,
                                                                            int maxResults)
        List the results of a query by a fixed batch size.
        Parameters:
        query - the query
        batchParam - a parameter of the query for batching
        list - a collection of values for the batch parameters to retrieve
        batchSize - the number of elements to fetch in each batch
        maxResults - maximum number of results to return, or -1 to ignore
      • listByIdentifiableBatch

        public static <S extends Identifiable,​T> List<T> listByIdentifiableBatch​(Query query,
                                                                                       String batchParam,
                                                                                       Collection<S> list,
                                                                                       int batchSize,
                                                                                       int maxResults)
      • stream

        public static <T> Stream<T> stream​(Query query,
                                           Class<T> resultType,
                                           int fetchSize,
                                           boolean useCursorFetchIfSupported,
                                           boolean isQueryStateless)
        Stream the result of a query with the given fetch size.

        If it is determined that setFetchSize() will not work, a strategy based on offset/limit will be used.

        Parameters:
        useCursorFetchIfSupported - if cursor fetching is supported by the JDBC driver, it will be used. This has implications on performance of the database server because the whole result set will be loaded in memory
        isQueryStateless - indicate if the query is stateless. A stateless query does not trigger any additional SQL statements upon being retrieved. If this is true, streaming will be enabled if useCursorFetchIfSupported is set to false.
      • stream

        public static <T> Stream<T> stream​(Criteria criteria,
                                           Class<T> resultType,
                                           int fetchSize,
                                           boolean useCursorFetchIfSupported,
                                           boolean isQueryStateless)
      • createStream

        public static <T> Stream<T> createStream​(SessionFactory sessionFactory,
                                                 Function<Session,​Stream<T>> streamFactory,
                                                 boolean createNewSession)
        Safely create a Stream from either the current or a new Session.
        Parameters:
        streamFactory - a function that produces the stream from a given Session. It may return null, in which case the session will be closed immediately
      • executeUpdateByBatch

        public static <S extends Comparable<S>> int executeUpdateByBatch​(Query query,
                                                                         String batchParam,
                                                                         Collection<S> list,
                                                                         int batchSize)
        Execute an update query by a fixed batch size.
        Returns:
        the sum of all performed update executions
        See Also:
        Query.executeUpdate()
      • escapeLike

        public static String escapeLike​(String s)