java.lang.Object
edu.internet2.middleware.grouper.hibernate.HibUtils

public class HibUtils extends Object
  • Constructor Details

    • HibUtils

      public HibUtils()
  • Method Details

    • attachBindValues

      public static void attachBindValues(org.hibernate.Query query, List<HibernateParam> bindVarNameParams)
      attach bind values to query
      Parameters:
      query -
    • paramsToString

      public static String paramsToString(Object params, Object types)
      Convert the params to friendly strings
      Parameters:
      params -
      types -
      Returns:
      the string of the params (for logging)
    • parseAlias

      public static String parseAlias(String text, boolean exceptionIfNotFound)
      Parses an alias out of a sql query.
      Parameters:
      text - is the text to find the alias in.
      exceptionIfNotFound - if true, when an alias is not found, and exception will be thrown.
      Returns:
      the alias.
    • equalsOrIs

      public static String equalsOrIs(Object value, String bindVar)
      if in an hql or sql query, depending on the value, pass is or = back
      Parameters:
      value -
      bindVar -
      Returns:
      the query comparator
    • assignDisallowCacheThreadLocal

      public static boolean assignDisallowCacheThreadLocal()
      if should disallow cache
        boolean needsUnassignment = HibUtils.assignDisallowCacheThreadLocal();
        Set attributeAssigns = null;
        try {
          attributeAssigns = retrieveAttributeAssignsByOwnerAndAttributeDefNameId(attributeDefName.getId());
        } finally {
          if (needsUnassignment) {
            HibUtils.clearDisallowCacheThreadLocal();
          }
        }
       
       
      Returns:
      if assigned
    • clearDisallowCacheThreadLocal

      public static void clearDisallowCacheThreadLocal()
      dont disallow cache anymore
    • secondLevelCaching

      public static boolean secondLevelCaching(Boolean cacheable, QueryOptions queryOptions)
      Parameters:
      cacheable -
      queryOptions -
      Returns:
      if caching
    • secondLevelCacheRegion

      public static String secondLevelCacheRegion(String cacheRegion, QueryOptions queryOptions)
      Parameters:
      cacheRegion -
      queryOptions -
      Returns:
      if caching
    • convertHqlToCountHql

      public static String convertHqlToCountHql(String hql)
      convert an hql to a count hql
      Parameters:
      hql -
      Returns:
      the hql of the count query
    • closeQuietly

      public static void closeQuietly(org.hibernate.ScrollableResults scrollableResults)
      Parameters:
      scrollableResults -
    • propertyIndex

      public static int propertyIndex(String[] propertyNames, String propertyName)
      find the property index based on property name
      Parameters:
      propertyNames -
      propertyName - e.g. userId
      Returns:
      the index (0 based) in the data arrays where the object is
    • assignProperty

      public static void assignProperty(Object[] state, String[] propertyNames, String propertyName, Object propertyValue)
      assign a property in hibernates arrays of states
      Parameters:
      state -
      propertyNames -
      propertyName -
      propertyValue -
    • propertyValue

      public static Object propertyValue(Object[] state, String[] propertyNames, String propertyName)
      find a property value in hibernates arrays of states
      Parameters:
      state -
      propertyNames -
      propertyName -
      Returns:
      the object
    • closeQuietly

      public static void closeQuietly(PreparedStatement preparedStatement)
      close a prepared statement
      Parameters:
      preparedStatement -
    • evict

      public static void evict(HibernateSession hibernateSession, Object object, boolean onlyEvictIfNotNew)
       evict a list of objects from hibernate.  do this always for two reasons:
       1. If you edit an object that is in the hibernate session, and commit, it will
       commit those changes magically.  Only objects called session.save(obj) or 
       update etc should be committed
       2. If you select an object, then try to store it back (but have a different
       reference, e.g. if the DTO went through it, then you will get an exception:
       "a different object with the same identifier value was already associated with the session"
       
      Parameters:
      hibernateSession - grouper hibernateSession, can be null if not known
      object - to evict that was just retrieved, can be list or array
      onlyEvictIfNotNew - true to only evict if this is a nested tx
    • evict

      public static void evict(HibernateSession hibernateSession, Object object, boolean onlyEvictIfNotNew, boolean evictBeforeFlush)
       evict a list of objects from hibernate.  do this always for two reasons:
       1. If you edit an object that is in the hibernate session, and commit, it will
       commit those changes magically.  Only objects called session.save(obj) or 
       update etc should be committed
       2. If you select an object, then try to store it back (but have a different
       reference, e.g. if the DTO went through it, then you will get an exception:
       "a different object with the same identifier value was already associated with the session"
       
      Parameters:
      hibernateSession - grouper hibernateSession, can be null if not known
      object - to evict that was just retrieved, can be list or array
      onlyEvictIfNotNew - true to only evict if this is a nested tx
      evictBeforeFlush - if evict before flush (dont do this if iterating through list)
    • evict

      public static void evict(HibernateSession hibernateSession, Collection<Object> list, boolean onlyEvictIfNotNew)
       evict a list of objects from hibernate.  do this always for two reasons:
       1. If you edit an object that is in the hibernate session, and commit, it will
       commit those changes magically.  Only objects called session.save(obj) or 
       update etc should be committed
       2. If you select an object, then try to store it back (but have a different
       reference, e.g. if the DTO went through it, then you will get an exception:
       "a different object with the same identifier value was already associated with the session"
       
      Parameters:
      hibernateSession - grouper hibernateSession
      list - of objects from hibernate to evict
      onlyEvictIfNotNew - true to only evict if this is a nested tx
    • listCrit

      public static org.hibernate.criterion.Criterion listCrit(org.hibernate.criterion.Criterion... criterions)
      make a list of criterions. e.g. listCrit(crit1, crit2, etc). will AND them together this is null and empty safe
      Parameters:
      criterions -
      Returns:
      the criterion containing the list or null if none passed in
    • listCrit

      public static org.hibernate.criterion.Criterion listCrit(List<org.hibernate.criterion.Criterion> criterions)
      make a list of criterions. e.g. listCrit(critList). will AND them together this is null and empty safe
      Parameters:
      criterions -
      Returns:
      the criterion containing the list or null if none passed in
    • listCritOr

      public static org.hibernate.criterion.Criterion listCritOr(org.hibernate.criterion.Criterion... criterions)
      make a list of criterions. e.g. listCrit(crit1, crit2, etc). will OR them together this is null and empty safe
      Parameters:
      criterions -
      Returns:
      the criterion containing the list or null if none passed in
    • listCritOr

      public static org.hibernate.criterion.Criterion listCritOr(List<org.hibernate.criterion.Criterion> criterions)
      make a list of criterions. e.g. listCrit(crits). will OR them together this is null and empty safe
      Parameters:
      criterions -
      Returns:
      the criterion containing the list or null if none passed in
    • closeQuietly

      public static void closeQuietly(Connection connection)
      close a connection null safe and dont throw exception
      Parameters:
      connection -
    • closeQuietly

      public static void closeQuietly(ResultSet resultSet)
      close a resultSet null safe and dont throw exception
      Parameters:
      resultSet -
    • closeQuietly

      public static void closeQuietly(org.hibernate.Session session)
      close a session null safe and dont throw exception
      Parameters:
      session -
    • closeQuietly

      public static void closeQuietly(Statement statement)
      close a statement null safe and dont throw exception
      Parameters:
      statement -
    • rollbackQuietly

      public static void rollbackQuietly(Connection connection)
      rollback a connection quietly
      Parameters:
      connection -
    • rollbackQuietly

      public static void rollbackQuietly(org.hibernate.Transaction transaction)
      rollback a transaction quietly
      Parameters:
      transaction -
    • attachParams

      @Deprecated public static void attachParams(PreparedStatement statement, Object params) throws org.hibernate.HibernateException, SQLException
      Deprecated.
      doesnt work with postgres
      Attach params for a prepared statement. The type of the params and types must be the same (e.g. either both array or list, but not one is Array, and the other list
      Parameters:
      statement -
      params - either null, Object, Object[], or List of Objects
      Throws:
      org.hibernate.HibernateException
      SQLException
    • listObject

      public static List<Object> listObject(Object object)
      convert an object to a list of objects
      Parameters:
      object -
      Returns:
      the list of objects
    • listType

      public static List<org.hibernate.type.Type> listType(org.hibernate.type.Type... types)
      convert types to a list of types return ObjectType.INSTANCE; DoubleType.INSTANCE, LongType.INSTANCE, FloatType.INSTANCE, ByteType.INSTANCE, TrueFalseType.INSTANCE, CharacterType.INSTANCE, ShortType.INSTANCE, DateType.INSTANCE, TimestampType.INSTANCE, StringType.INSTANCE;
      Parameters:
      types -
      Returns:
      the list of objects
    • hibernateType

      @Deprecated public static org.hibernate.type.Type hibernateType(Object o)
      Deprecated.
      doesnt work with postgres
      Returns a Hibernate Type for the given java type. Handles both primitives and Objects. Will throw an exception if the given object is null or if a type cannot be found for it.
      Parameters:
      o - is the object to find the Type for.
      Returns:
      the Type.
    • hibernateTypes

      @Deprecated public static List<org.hibernate.type.Type> hibernateTypes(List<Object> params)
      Deprecated.
      doesnt work with postgres
      Returns a list of Hibernate types corresponding to the given params.
      Parameters:
      params - are the objects to get the types for. Can be list, Object, or array.
      Returns:
      the corresponding types.
    • convertToInClause

      public static String convertToInClause(Collection<String> collection, HqlQuery scalarable)
      convert a collection of strings (no parens) to an in clause
      Parameters:
      collection -
      scalarable - to set the string
      Returns:
      the string of in clause (without parens)
    • convertToInClauseAnyType

      public static String convertToInClauseAnyType(Collection<?> collection, HqlQuery scalarable)
      convert a collection of strings (no parens) to an in clause
      Parameters:
      collection -
      scalarable - to set the string
      Returns:
      the string of in clause (without parens)
    • convertToSubjectInClause

      public static String convertToSubjectInClause(Collection<Subject> subjects, HqlQuery hqlQuery, String memberAlias)
      Parameters:
      memberAlias - is the alias of the table for members, e.g. gm
      subjects - collection of subjects
      hqlQuery - so far
      Returns:
      the query, e.g. ((gm.subject_id = '123' and gm.subject_source = 'jdbc') or (gm.subject_id = '234' and gm.subject_source = 'jdbc' ))
    • escapeSqlString

      public static String escapeSqlString(String input)
      escape the quotes from sql string
      Parameters:
      input -
      Returns:
      the escaped string
    • convertToInClauseForSqlStatic

      public static String convertToInClauseForSqlStatic(Collection<String> collection)
      convert a collection of strings (no parens) to an in clause
      Parameters:
      collection -
      Returns:
      the string of in clause (without parens)
    • buildInCriterion

      public static org.hibernate.criterion.Criterion buildInCriterion(String propertyName, List<?> values, int inStatementSizeLimit)
      Creates a Hibernate Criterion that breaks a (possibly very long) list of values into an ORed series of IN statements, respecting the IN-statement-size-limit provided. See: https://thephilosophicalfacts.wordpress.com/2015/06/22/hibernate-criteria-list-size-is-more-than-1000-values/
      Parameters:
      propertyName -
      values -
      inStatementSizeLimit -
      Returns:
    • convertToMultiKeyInClause

      public static void convertToMultiKeyInClause(Collection<MultiKey> collection, HqlQuery scalarable, Collection<String> columnNames, StringBuilder whereClause)
      convert a collection of multikeys to an in clause with multiple args. currently this only works with strings, though we could add support for more types in future
      Parameters:
      collection -
      scalarable - to set the string
      columnNames - names of columns in multikey
      whereClause -
    • convertFieldsToSqlInString

      public static void convertFieldsToSqlInString(Collection<Field> fields, HqlQuery hqlQuery, StringBuilder sql, String fieldColumnName)
      Parameters:
      fields -
      hqlQuery -
      sql - before the and statement
      fieldColumnName -
    • convertSourcesToSqlInString

      public static void convertSourcesToSqlInString(Set<Source> sources, HqlQuery hqlQuery, StringBuilder sql, String sourceColumnName)
      Parameters:
      sources -
      hqlQuery -
      sql - before the and statement
      sourceColumnName -
    • convertSourcesToSqlInString

      public static String convertSourcesToSqlInString(Set<Source> sources)
      e.g. ('g:gsa', 'jdbc')
      Parameters:
      sources -
      Returns:
      the in string, of sources sorted alphabetically