Class HibUtils
java.lang.Object
edu.internet2.middleware.grouper.hibernate.HibUtils
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
if should disallow cachestatic void
assignProperty
(Object[] state, String[] propertyNames, String propertyName, Object propertyValue) assign a property in hibernates arrays of statesstatic void
attachBindValues
(org.hibernate.Query query, List<HibernateParam> bindVarNameParams) attach bind values to querystatic void
attachParams
(PreparedStatement statement, Object params) Deprecated.doesnt work with postgresstatic 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.static void
dont disallow cache anymorestatic void
closeQuietly
(Connection connection) close a connection null safe and dont throw exceptionstatic void
closeQuietly
(PreparedStatement preparedStatement) close a prepared statementstatic void
closeQuietly
(ResultSet resultSet) close a resultSet null safe and dont throw exceptionstatic void
closeQuietly
(Statement statement) close a statement null safe and dont throw exceptionstatic void
closeQuietly
(org.hibernate.ScrollableResults scrollableResults) static void
closeQuietly
(org.hibernate.Session session) close a session null safe and dont throw exceptionstatic void
convertFieldsToSqlInString
(Collection<Field> fields, HqlQuery hqlQuery, StringBuilder sql, String fieldColumnName) static String
convert an hql to a count hqlstatic String
convertSourcesToSqlInString
(Set<Source> sources) e.g.static void
convertSourcesToSqlInString
(Set<Source> sources, HqlQuery hqlQuery, StringBuilder sql, String sourceColumnName) static String
convertToInClause
(Collection<String> collection, HqlQuery scalarable) convert a collection of strings (no parens) to an in clausestatic String
convertToInClauseAnyType
(Collection<?> collection, HqlQuery scalarable) convert a collection of strings (no parens) to an in clausestatic String
convertToInClauseForSqlStatic
(Collection<String> collection) convert a collection of strings (no parens) to an in clausestatic void
convertToMultiKeyInClause
(Collection<MultiKey> collection, HqlQuery scalarable, Collection<String> columnNames, StringBuilder whereClause) convert a collection of multikeys to an in clause with multiple args.static String
convertToSubjectInClause
(Collection<Subject> subjects, HqlQuery hqlQuery, String memberAlias) static String
equalsOrIs
(Object value, String bindVar) if in an hql or sql query, depending on the value, pass is or = backstatic String
escapeSqlString
(String input) escape the quotes from sql stringstatic void
evict
(HibernateSession hibernateSession, Object object, boolean onlyEvictIfNotNew) evict a list of objects from hibernate.static void
evict
(HibernateSession hibernateSession, Object object, boolean onlyEvictIfNotNew, boolean evictBeforeFlush) evict a list of objects from hibernate.static void
evict
(HibernateSession hibernateSession, Collection<Object> list, boolean onlyEvictIfNotNew) evict a list of objects from hibernate.static org.hibernate.type.Type
Deprecated.doesnt work with postgresstatic List<org.hibernate.type.Type>
hibernateTypes
(List<Object> params) Deprecated.doesnt work with postgresstatic org.hibernate.criterion.Criterion
make a list of criterions.static org.hibernate.criterion.Criterion
listCrit
(org.hibernate.criterion.Criterion... criterions) make a list of criterions.static org.hibernate.criterion.Criterion
listCritOr
(List<org.hibernate.criterion.Criterion> criterions) make a list of criterions.static org.hibernate.criterion.Criterion
listCritOr
(org.hibernate.criterion.Criterion... criterions) make a list of criterions.listObject
(Object object) convert an object to a list of objectsstatic 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;static String
paramsToString
(Object params, Object types) Convert the params to friendly stringsstatic String
parseAlias
(String text, boolean exceptionIfNotFound) Parses an alias out of a sql query.static int
propertyIndex
(String[] propertyNames, String propertyName) find the property index based on property namestatic Object
propertyValue
(Object[] state, String[] propertyNames, String propertyName) find a property value in hibernates arrays of statesstatic void
rollbackQuietly
(Connection connection) rollback a connection quietlystatic void
rollbackQuietly
(org.hibernate.Transaction transaction) rollback a transaction quietlystatic String
secondLevelCacheRegion
(String cacheRegion, QueryOptions queryOptions) static boolean
secondLevelCaching
(Boolean cacheable, QueryOptions queryOptions)
-
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
Convert the params to friendly strings- Parameters:
params
-types
-- Returns:
- the string of the params (for logging)
-
parseAlias
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
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 cacheboolean 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
- Parameters:
cacheable
-queryOptions
-- Returns:
- if caching
-
secondLevelCacheRegion
- Parameters:
cacheRegion
-queryOptions
-- Returns:
- if caching
-
convertHqlToCountHql
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
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
find a property value in hibernates arrays of states- Parameters:
state
-propertyNames
-propertyName
-- Returns:
- the object
-
closeQuietly
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 knownobject
- to evict that was just retrieved, can be list or arrayonlyEvictIfNotNew
- 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 knownobject
- to evict that was just retrieved, can be list or arrayonlyEvictIfNotNew
- true to only evict if this is a nested txevictBeforeFlush
- 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 hibernateSessionlist
- of objects from hibernate to evictonlyEvictIfNotNew
- 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
close a connection null safe and dont throw exception- Parameters:
connection
-
-
closeQuietly
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
close a statement null safe and dont throw exception- Parameters:
statement
-
-
rollbackQuietly
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 postgresAttach 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
convert an object to a list of objects- Parameters:
object
-- Returns:
- the list of objects
-
listType
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.doesnt work with postgresReturns 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.doesnt work with postgresReturns 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
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
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. gmsubjects
- collection of subjectshqlQuery
- 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
escape the quotes from sql string- Parameters:
input
-- Returns:
- the escaped string
-
convertToInClauseForSqlStatic
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 stringcolumnNames
- names of columns in multikeywhereClause
-
-
convertFieldsToSqlInString
public static void convertFieldsToSqlInString(Collection<Field> fields, HqlQuery hqlQuery, StringBuilder sql, String fieldColumnName) - Parameters:
fields
-hqlQuery
-sql
- before the and statementfieldColumnName
-
-
convertSourcesToSqlInString
public static void convertSourcesToSqlInString(Set<Source> sources, HqlQuery hqlQuery, StringBuilder sql, String sourceColumnName) - Parameters:
sources
-hqlQuery
-sql
- before the and statementsourceColumnName
-
-
convertSourcesToSqlInString
e.g. ('g:gsa', 'jdbc')- Parameters:
sources
-- Returns:
- the in string, of sources sorted alphabetically
-