Class QueryUtils

java.lang.Object
ubic.gemma.persistence.util.QueryUtils

public class QueryUtils extends Object
Utilities for Hibernate Query.

Pre-phase-2 this class also accepted legacy org.hibernate.Criteria inputs and tinkered with the MySQL JDBC driver to enable cursor-based streaming. Hibernate 6 removed the Criteria API and the Hibernate-internal accessors that streaming hack relied on. Streaming now uses Query.scroll(ScrollMode) when supported and an offset/limit fallback otherwise. Cursor-fetch tuning can be re-added if needed.

  • Field Details

  • Constructor Details

    • QueryUtils

      public QueryUtils()
  • Method Details

    • list

      public static <T> List<T> list(org.hibernate.query.Query<?> query)
    • setMaxResultsIfPositive

      public static org.hibernate.query.Query<?> setMaxResultsIfPositive(org.hibernate.query.Query<?> query, int maxResults)
      Apply Query.setMaxResults(int) only when maxResults is positive.

      Hibernate 5+ rejects setMaxResults(<0); many Gemma DAOs pass -1 to mean "no limit".

    • uniqueResult

      public static <T> T uniqueResult(org.hibernate.query.Query<?> query)
    • 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.
    • optimizeIdentifiableParameterList

      public static <T extends Identifiable> Collection<T> optimizeIdentifiableParameterList(Collection<T> list)
      Optimize a collection of Identifiable entities by ID.
    • 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.
    • batchIdentifiableParameterList

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

      public static <S extends Comparable<S>, T> List<T> listByBatch(org.hibernate.query.Query<?> query, String batchParam, Collection<S> list, int batchSize)
    • listByBatch

      public static <S extends Comparable<S>, T> List<T> listByBatch(org.hibernate.query.Query<?> query, String batchParam, Collection<S> list, int batchSize, int maxResults)
    • listByIdentifiableBatch

      public static <S extends Identifiable, T> List<T> listByIdentifiableBatch(org.hibernate.query.Query<?> query, String batchParam, Collection<S> list, int batchSize)
    • listByIdentifiableBatch

      public static <S extends Identifiable, T> List<T> listByIdentifiableBatch(org.hibernate.query.Query<?> query, String batchParam, Collection<S> list, int batchSize, int maxResults)
    • streamByBatch

      public static <S extends Comparable<S>, T> Stream<T> streamByBatch(org.hibernate.query.Query<?> query, String batchParam, Collection<S> list, int batchSize)
    • streamByIdentifiableBatch

      public static <S extends Identifiable, T> Stream<T> streamByIdentifiableBatch(org.hibernate.query.Query<?> query, String batchParam, Collection<S> list, int batchSize)
    • stream

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

      Uses Query.scroll(ScrollMode) when possible, otherwise falls back to offset/limit pagination. The useCursorFetchIfSupported and isQueryStateless hints are accepted for source compatibility but no longer dispatch on driver-specific tricks.

    • createStream

      @Nullable 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.
    • executeUpdateByBatch

      public static <S extends Comparable<S>> int executeUpdateByBatch(org.hibernate.query.Query<?> query, String batchParam, Collection<S> list, int batchSize)
    • escapeLike

      public static String escapeLike(String s)
    • escapeLike

      public static String escapeLike(String s, char escapeChar)
      Escape the SQL LIKE wildcards in s using an explicit escape character.

      Both wildcards must be escaped, _ as much as % — _ matches any single character, so leaving it raw makes a search for 1007_s_at quietly match neighbours. The escape character itself is escaped too, so it is safe for it to occur in the data.

      The caller MUST emit a matching escape clause. Relying on the backslash default is not enough: under MySQL's NO_BACKSLASH_ESCAPES mode a backslash is an ordinary character, and the escaped pattern then matches nothing at all rather than matching too much — which is exactly how underscore searches came to return empty results everywhere.