edu.internet2.middleware.grouper.hibernate
Class HibUtils

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

public class HibUtils
extends Object

Author:
mchyzer

Constructor Summary
HibUtils()
           
 
Method Summary
static void assignProperty(Object[] state, String[] propertyNames, String propertyName, Object propertyValue)
          assign a property in hibernates arrays of states
static void attachParams(PreparedStatement statement, Object params)
          Attach params for a prepared statement.
static void closeQuietly(Connection connection)
          close a connection null safe and dont throw exception
static void closeQuietly(PreparedStatement preparedStatement)
          close a prepared statement
static void closeQuietly(ResultSet resultSet)
          close a resultSet null safe and dont throw exception
static void closeQuietly(org.hibernate.ScrollableResults scrollableResults)
           
static void closeQuietly(org.hibernate.Session session)
          close a session null safe and dont throw exception
static void closeQuietly(Statement statement)
          close a statement null safe and dont throw exception
static String convertHqlToCountHql(String hql)
          convert an hql to a count hql
static String convertToInClause(Collection<String> collection, HqlQuery scalarable)
          convert a collection of strings (no parens) to an in clause
static String convertToInClauseForSqlStatic(Collection<String> collection)
          convert a collection of strings (no parens) to an in clause
static void evict(HibernateSession hibernateSession, Collection<Object> list, boolean onlyEvictIfNotNew)
           evict a list of objects from hibernate.
static void evict(HibernateSession hibernateSession, Object object, boolean onlyEvictIfNotNew)
           evict a list of objects from hibernate.
static org.hibernate.type.Type hibernateType(Object o)
          Returns a Hibernate Type for the given java type.
static List<org.hibernate.type.Type> hibernateTypes(List<Object> params)
          Returns a list of Hibernate types corresponding to the given params.
static org.hibernate.criterion.Criterion listCrit(org.hibernate.criterion.Criterion... criterions)
          make a list of criterions.
static org.hibernate.criterion.Criterion listCrit(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.
static org.hibernate.criterion.Criterion listCritOr(List<org.hibernate.criterion.Criterion> criterions)
          make a list of criterions.
static List<Object> listObject(Object object)
          convert an object to a list of objects
static int propertyIndex(String[] propertyNames, String propertyName)
          find the property index based on property name
static Object propertyValue(Object[] state, String[] propertyNames, String propertyName)
          find a property value in hibernates arrays of states
static void rollbackQuietly(Connection connection)
          rollback a connection quietly
static void rollbackQuietly(org.hibernate.Transaction transaction)
          rollback a transaction quietly
static String secondLevelCacheRegion(String cacheRegion, QueryOptions queryOptions)
           
static boolean secondLevelCaching(Boolean cacheable, QueryOptions queryOptions)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibUtils

public HibUtils()
Method Detail

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,
                         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

public static void attachParams(PreparedStatement statement,
                                Object params)
                         throws org.hibernate.HibernateException,
                                SQLException
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

hibernateType

public static org.hibernate.type.Type hibernateType(Object o)
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

public static List<org.hibernate.type.Type> hibernateTypes(List<Object> params)
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)

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)