com.jalios.jcms.db
Class HibernateUtil

java.lang.Object
  extended by com.jalios.jcms.db.HibernateUtil
All Implemented Interfaces:
DBConstants, JaliosConstants

public class HibernateUtil
extends Object
implements DBConstants, JaliosConstants

Helper class providing static methods to perform common database operation using hibertnate

Since:
jcms-6.0
Version:
$Revision: 34310 $

Field Summary
static int DEFAULT_BATCH_SIZE
           
static String REVISION
           
 
Fields inherited from interface com.jalios.jcms.db.DBConstants
ALL_CATEGORIES_SET_FIELD, AUTH_GRP_SET_FIELD, AUTH_MBR_SET_FIELD, AUTHOR_ID_FIELD, CATEGORIES_SET_FIELD, CDATE_FIELD, CHANNEL_JCMSDB_PROP, DBDATA_URID, HAS_NO_READ_RIGHTS, HIBERNATE_MAPPING_ADATE, HIBERNATE_MAPPING_AUTHOR, HIBERNATE_MAPPING_CDATE, HIBERNATE_MAPPING_EDATE, HIBERNATE_MAPPING_MDATE, HIBERNATE_MAPPING_PDATE, HIBERNATE_MAPPING_PSTATUS, HIBERNATE_MAPPING_TITLE, HIBERNATE_MAPPING_WORKSPACE_ID, MDATE_FIELD, PSTATUS_FIELD, ROW_ID_FIELD, SCROLL_FETCH_SIZE, WORKSPACE_ID_FIELD
 
Fields inherited from interface com.jalios.util.JaliosConstants
CRLF, MILLIS_IN_ONE_DAY, MILLIS_IN_ONE_HOUR, MILLIS_IN_ONE_MINUTE, MILLIS_IN_ONE_MONTH, MILLIS_IN_ONE_SECOND, MILLIS_IN_ONE_WEEK, MILLIS_IN_ONE_YEAR
 
Constructor Summary
HibernateUtil()
           
 
Method Summary
static void beginTransaction()
          Begins a new transaction in the session of the current thread.
static org.hibernate.criterion.Criterion buildAndCriterion(org.hibernate.criterion.Criterion c1, org.hibernate.criterion.Criterion c2)
          Returns the AND of the two criterion.
static org.hibernate.criterion.Criterion buildCollectionCriterion(Class clazz, String property, Object value, org.hibernate.type.Type valueType)
          Apply a constraint on a value of the collection of the given clazz.
static org.hibernate.criterion.Criterion buildCollectionCriterion(Class clazz, String property, String value)
          Apply a constraint on a value of the collection of the given clazz.
static org.hibernate.criterion.Criterion buildCollectionCriterion(String entityTable, String idColumn, String collectionTable, String itemIdColumn, String valueColumn, Object value, org.hibernate.type.Type valueType)
          Apply a constraint on a value of the collection of the given entity.
static String buildJcmsId(Class<?> clazz, Long rowId)
          Generates the internal JCMS id for the given database object.
static org.hibernate.criterion.Criterion buildOrCriterion(org.hibernate.criterion.Criterion c1, org.hibernate.criterion.Criterion c2)
          Returns the OR of the two criterion.
static
<T> org.hibernate.Query
buildQueryCollection(Class<? extends T> clazz, String property, Object value, boolean retreiveRowId)
          Returns the Hibernate query to search the instance of the given class with the collection property matching the given value.
static org.hibernate.Criteria buildQueryCriteria(org.hibernate.Session session, Class<?> clazz, String[] properties, Object[] values, String orderBy, int firstResult, int maxResults)
          Retrieves the result(s) matching the specified parameters.
static String checkSQLInjection(String... strings)
          Checks if the given strings may contains an SQL injection.
static void clearCache()
           
static void closeSession()
          Close the hibernate session of the current thread.
static void commitTransaction()
          Commits the transaction of the current thread if it exists.
static void delete(DBData data)
          Deletes the specified object in the database.
static void delete(DBData data, boolean cleanSession)
          Deletes the specified object in the database.
static void delete(String id)
          Deletes the specified object in the database.
static int deleteQuery(Class<?> clazz, String[] properties, String[] values)
          Delete all object matching the specified parameters.
static int deleteQuery(Class<?> clazz, String[] properties, String[] values, boolean cleanSession)
          Delete all object matching the specified parameters.
static int deleteQuery(Class<?> clazz, String property, String value)
          Delete all object matching the specified parameters.
static int deleteQuery(Class<?> clazz, String property, String value, boolean cleanSession)
          Delete all object matching the specified parameters.
static void evict(Object obj)
          Removes this instance from the Hibernate session cache to avoid automatic persistence by dirty checking.
static List execQuery(org.hibernate.Query query)
          Execute the specified hibernate query using the current thread's session.
static List execQuery(String hql)
          Execute the specified hibernate query using the current thread's session.
static List execQuery(String hql, int firstResult, int maxResults)
          Execute the specified hibernate query using the current thread's session.
static Iterator execQueryIterator(org.hibernate.Query query)
          Execute the specified hibernate query using the current thread's session.
static Iterator execQueryIterator(String hql)
          Execute the specified hibernate query using the current thread's session.
static Object execQueryUnique(String hql)
          Execute the specified hibernate query using the current thread's session.
static int execUpdate(org.hibernate.Query query)
          Execute the update or delete query.
static int execUpdate(String hql)
          Execute the update or delete HQL statement.
static LinkedHashMap<String,Float> filterSearchResultByIdList(LinkedHashMap<String,Float> resultMap, List<String> idList)
          Filters the lucene search result with the given list of publication's identifier.
static Set<Set<Long>> getBatchSet(Collection<Long> idColl, int batchSize)
          Convenient method to convert a Collection of rowId into many smaller collections, allowing batch select using method getDBDataList(Class, Collection).
static Data getCurrentData(String id)
          Return the current persisted state of the Data with the given id.
static Date getCurrentDate()
          Returns a new date.
static DBData getCurrentDBData(String id)
          Return the current persisted state of the DBData with the given id.
static Object getCurrentObject(Class clazz, Long rowId)
          Return the current persisted state of the object with the given rowId.
static Data getData(String id)
          Retrieves the Data bound to the specified jcms id from the database.
static DBData getDBData(String id)
          Retrieves the DBData bound to the specified jcms id from the database.
static
<T> Iterator<T>
getDBDataIterator(Class<T> clazz)
          Retrieve all Objects of the specified class from the database.
static
<T> List<T>
getDBDataList(Class<T> clazz)
          Retrieve all Objects of the specified class from the database (limited to maxResults).
static
<T> List<T>
getDBDataList(Class<T> clazz, Collection<Long> rowIdColl)
          Retrieve a List of Objects from their Class and rowId.
static
<T> List<T>
getDBDataList(Class<T> clazz, int firstResult, int maxResults)
          Retrieve all Objects of the specified class from the database.
static Object getObject(Class clazz, Long rowId)
          Retrieves the Object of the specified class and using the specified id.
static Object getObject(org.hibernate.Session session, Class clazz, Long rowId)
          Retrieves the Object of the specified class and using the specified id in the given session.
static Long getRowId(String id)
          Retrieve the row id from a DBData id
static Set<Set<Long>> getRowIdBatchSet(Collection<String> idColl, int batchSize)
          Convenient method to convert a Collection of id into many smaller collections, allowing batch select using method getDBDataList(Class, Collection).
static org.hibernate.Session getSession()
          Retrieve and open an hibernate session for the current thread
static org.hibernate.SessionFactory getSessionFactory()
          Retrieve the Hibernate SessionFactory singleton
static void handleTimeoutException(Throwable th)
          Handle time out exception
static boolean isDBDataJcmsId(String id)
           
static Object[] parseJcmsId(String id)
          Returns the 2 component of the given JCMS ID.
static
<T> List<T>
query(Class<? extends T> clazz, Map<String,Object> paramsMap)
          Retrieves all object matching the specified parameters.
static
<T> List<T>
query(Class<? extends T> clazz, Map<String,Object> paramsMap, String orderBy)
          Retrieves all object matching the specified parameters.
static
<T> List<T>
query(Class<? extends T> clazz, Map<String,Object> paramsMap, String orderBy, int firstResult, int maxResults)
          Retrieves all object matching the specified parameters.
static
<T> List<T>
query(Class<? extends T> clazz, String orderBy)
          Retrieves all object of the specified Class.
static
<T> List<T>
query(Class<? extends T> clazz, String[] properties, Object[] values)
          Retrieves all object matching the specified parameters.
static
<T> List<T>
query(Class<? extends T> clazz, String[] properties, Object[] values, String orderBy)
          Retrieves all object matching the specified parameters.
static
<T> List<T>
query(Class<? extends T> clazz, String property, Object value)
          Retrieves all object matching the specified parameters.
static
<T> List<T>
query(Class<? extends T> clazz, String property, Object value, String orderBy)
          Retrieves all object matching the specified parameters.
static
<T> List<T>
query(Class<T> clazz)
          Retrieves all object of the specified Class.
static
<T> List<T>
queryCollection(Class<? extends T> clazz, String property, Object value)
          Retreives the instance of the given class with the collection property matching the given value.
static
<T> List<Long>
queryCollectionRowId(Class<? extends T> clazz, String property, Object value)
          Retreives the instance of the given class with the collection property matching the given value.
static int queryCount(Class<?> clazz)
          Returns the count of objects matching the given class.
static int queryCount(Class<?> clazz, org.hibernate.criterion.Criterion criterion)
          Returns the count of objects matching the given class with the given criterion.
static int queryCount(Class<?> clazz, String[] properties, Object[] values)
          Returns the count of objects matching the given class with the given criteria.
static int queryCount(Class<?> clazz, String property, Object value)
          Returns the count of objects matching the given class with the given criteria.
static int queryCount(org.hibernate.Criteria criteria)
          Returns the count of objects matching the given criteria.
static PageResult<Publication> queryPublication(PublicationCriteria pubCriteria)
          Returns a list of publications in the database matching the given criteria.
static
<T> T
queryUnique(Class<? extends T> clazz, String[] properties, Object[] values)
          Retrieves a unique object matching the specified parameters.
static
<T> T
queryUnique(Class<? extends T> clazz, String[] properties, Object[] values, String orderBy)
          Retrieves a unique object matching the specified parameters.
static
<T> T
queryUnique(Class<? extends T> clazz, String property, Object value)
          Retrieves a unique object matching the specified parameters.
static void removeSecondLevelCacheEntry(String entityName, Long rowId)
          Removes an DBData from the second level cache.
static void rollbackTransaction()
          Rollback the transaction of the current thread if it exists.
static void save(Data data)
          Saves or updates the specified Data in the database.
static void save(Object obj)
          Saves or updates the specified Object in the database.
static org.hibernate.ScrollableResults scroll(Class clazz)
          Scroll over a class.
static org.hibernate.ScrollableResults scroll(Class clazz, org.hibernate.ScrollMode scrollMode)
          Scroll over a class with a given scroll mode.
static
<T> org.hibernate.ScrollableResults
scrollCollection(Class<? extends T> clazz, String property, Object value, boolean retreiveRowId)
          Scroll the instance of the given class with the collection property matching the given value.
static boolean supportDateMillis()
          Returns true if the database supports millis for date.
static boolean supportFullJoin()
          Returns true if the database supports full join.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values

DEFAULT_BATCH_SIZE

public static final int DEFAULT_BATCH_SIZE
See Also:
Constant Field Values
Constructor Detail

HibernateUtil

public HibernateUtil()
Method Detail

getSessionFactory

public static org.hibernate.SessionFactory getSessionFactory()
Retrieve the Hibernate SessionFactory singleton

Returns:
the SessionFactory singleton
Since:
jcms-6.0

getSession

public static org.hibernate.Session getSession()
Retrieve and open an hibernate session for the current thread

Returns:
an opened Session ready to be used for hibernate transaction
Since:
jcms-6.0

closeSession

public static void closeSession()
Close the hibernate session of the current thread.

Since:
jcms-6.0

beginTransaction

public static void beginTransaction()
Begins a new transaction in the session of the current thread.

Since:
jcms-6.0

commitTransaction

public static void commitTransaction()
Commits the transaction of the current thread if it exists.

Since:
jcms-6.0

rollbackTransaction

public static void rollbackTransaction()
Rollback the transaction of the current thread if it exists.

Since:
jcms-6.0

buildJcmsId

public static String buildJcmsId(Class<?> clazz,
                                 Long rowId)
Generates the internal JCMS id for the given database object.

Parameters:
clazz - the Class of the object (required)
rowId - the row id in the database table (required)
Returns:
the internal JCMS id for the given database object.
Throws:
IllegalArgumentException - if clazz or rowId is null
Since:
jcms-6.0.0

isDBDataJcmsId

public static boolean isDBDataJcmsId(String id)

execQueryUnique

public static Object execQueryUnique(String hql)
Execute the specified hibernate query using the current thread's session.

Return the unique query result. If the query contains multiple results per row, the results are returned in an instance of Object[].

Parameters:
hql - the query as a string using the hibernate query language
Returns:
the result or null if it could not be found (or if an error occured)
Since:
jcms-6.0

execQuery

public static List execQuery(String hql)
Execute the specified hibernate query using the current thread's session.

Return the query results as a List. If the query contains multiple results per row, the results are returned in an instance of Object[].

Parameters:
hql - the query as a string using the hibernate query language
Returns:
the result list (never return null)
Since:
jcms-6.0

execQuery

public static List execQuery(String hql,
                             int firstResult,
                             int maxResults)
Execute the specified hibernate query using the current thread's session.

Return the query results as a List. If the query contains multiple results per row, the results are returned in an instance of Object[].

Parameters:
hql - the query as a string using the hibernate query language
firstResult - set the first data to retrieve. If <= 0, rows will be retrieved beginnning from first data (does not work with query on super classes).
maxResults - set the maximum number of data to retrieve. If < 0, there is no limit to the number of data retrieved (does not work with query on super classes).
Returns:
the result list (never return null)
Since:
jcms-6.0

execQuery

public static List execQuery(org.hibernate.Query query)
Execute the specified hibernate query using the current thread's session.

Return the query results as a List. If the query contains multiple results per row, the results are returned in an instance of Object[].

Parameters:
query - the hibernate query
Returns:
the result list (never return null)
Since:
jcms-6.0

execQueryIterator

public static Iterator execQueryIterator(String hql)
Execute the specified hibernate query using the current thread's session.

Return the query results as an Iterator. If the query contains multiple results pre row, the results are returned in an instance of Object[].
Entities returned as results are initialized on demand. The first SQL query returns identifiers only.

Parameters:
hql - the query as a string using the hibernate query language
Returns:
the result iterator (never return null)
Since:
jcms-6.0

execQueryIterator

public static Iterator execQueryIterator(org.hibernate.Query query)
Execute the specified hibernate query using the current thread's session.

Return the query results as an Iterator. If the query contains multiple results pre row, the results are returned in an instance of Object[].
Entities returned as results are initialized on demand. The first SQL query returns identifiers only.

Parameters:
query - the hibernate query
Returns:
the result iterator
Since:
jcms-6.0

execUpdate

public static int execUpdate(String hql)
Execute the update or delete HQL statement.

Parameters:
hql - the query as a string using the hibernate query language
Returns:
the number of entities updated or deleted.
Since:
jcms-6.0

execUpdate

public static int execUpdate(org.hibernate.Query query)
Execute the update or delete query.

Parameters:
query - the Hibernate query
Returns:
the number of entities updated or deleted.
Since:
jcms-6.0

query

public static <T> List<T> query(Class<T> clazz)
Retrieves all object of the specified Class.

Example to retrieve all ArchivedPublication.

   HibernateUtil.query(ArchivedPublication.class);
 
Results will be sorted by cdate if the Class is a subclass of Data, otherwise, results are not sorted.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
Returns:
a list of all object found in the database (never return null)
Throws:
IllegalArgumentException - if clazz is null
Since:
jcms-6.0

query

public static <T> List<T> query(Class<? extends T> clazz,
                                String orderBy)
Retrieves all object of the specified Class.

Example to retrieve all ArchivedPublication order by rowId.

   HibernateUtil.query(ArchivedPublication.class, "rowId");
 

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
orderBy - the names of the fields to be used to order the results (comma separeted). You can append "asc" or "desc" to indicate the order direction. Default is ascending.
Returns:
a list of all object found in the database (never return null)
Throws:
IllegalArgumentException - if clazz is null
Since:
jcms-6.0

query

public static <T> List<T> query(Class<? extends T> clazz,
                                String property,
                                Object value)
Retrieves all object matching the specified parameters.

Example to retrieve all ReaderTracker of a publication

   HibernateUtil.query(ReaderTracker.class, "publicationId", pub.getId());
 
Results will be sorted by cdate if the Class is a subclass of Data, otherwise, results are not sorted.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
property - a field name. All results are return if null.
value - the value of the field to match. All results are return if null.
Returns:
a list of all object found in the database (never return null)
Throws:
IllegalArgumentException - if clazz is null
Since:
jcms-6.0

query

public static <T> List<T> query(Class<? extends T> clazz,
                                String property,
                                Object value,
                                String orderBy)
Retrieves all object matching the specified parameters.

Example to retrieve all ExtraDBData of a member, order by extradbdata name

   HibernateUtil.query(ExtraDBData.class, "dataId", mbr.getId(), "extraName");
 

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
property - a field name. All results are return if null.
value - the value of the field to match. All results are return if null.
orderBy - the names of the fields to be used to order the results (comma separated). You can append "asc" or "desc" to indicate the order direction. Default is ascending.
Returns:
a list of all object found in the database (never return null)
Throws:
IllegalArgumentException - if clazz is null
Since:
jcms-6.0

query

public static <T> List<T> query(Class<? extends T> clazz,
                                String[] properties,
                                Object[] values)
Retrieves all object matching the specified parameters.

Example to retrieve ReaderTracker of a publication for a specific member.

   String[] properties = new String[] {"publicationId", "authorId"};
   String[] values = new String[] {pub.getId(), mbr.getId()};
   HibernateUtil.query(ReaderTracker.class, properties, values);
 
Results will be sorted by cdate if the Class is a subclass of Data, otherwise, results are not sorted

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
properties - an array of fields name. All results are return if null or empty.
values - an array of values to match, must be the same size as properties. All results are return if null or empty.
Returns:
a list of all object found in the database (never return null)
Throws:
IllegalArgumentException - if clazz is null or if propreties and values are not of the same length
Since:
jcms-6.0

query

public static <T> List<T> query(Class<? extends T> clazz,
                                String[] properties,
                                Object[] values,
                                String orderBy)
Retrieves all object matching the specified parameters.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
properties - an array of fields name. All results are return if null or empty.
values - an array of values to match, must be the same size as properties. All results are return if null or empty.
orderBy - the names of the fields to be used to order the results (comma separated). You can append "asc" or "desc" to indicate the order direction. Default is ascending.
Returns:
a list of all object found in the database
Throws:
IllegalArgumentException - if clazz is null or if propreties and values are not of the same length
Since:
jcms-6.0

queryUnique

public static <T> T queryUnique(Class<? extends T> clazz,
                                String property,
                                Object value)
Retrieves a unique object matching the specified parameters.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required
property - a field name. All results are return if null.
value - the value of the field to match. All results are return if null.
Returns:
the object found in the database
Throws:
IllegalArgumentException - if clazz is null
Since:
jcms-6.0

queryUnique

public static <T> T queryUnique(Class<? extends T> clazz,
                                String[] properties,
                                Object[] values)
Retrieves a unique object matching the specified parameters.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required
properties - an array of fields name. All results are return if null or empty.
values - an array of values to match, must be the same size as properties. All results are return if null or empty.
Returns:
the object found in the database
Throws:
IllegalArgumentException - if clazz is null or if propreties and values are not of the same length
Since:
jcms-6.0

queryUnique

public static <T> T queryUnique(Class<? extends T> clazz,
                                String[] properties,
                                Object[] values,
                                String orderBy)
Retrieves a unique object matching the specified parameters.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required
properties - an array of fields name. All results are return if null or empty.
values - an array of values to match, must be the same size as properties. All results are return if null or empty.
orderBy - the names of the fields to be used to order the results (comma separated). You can append "asc" or "desc" to indicate the order direction. Default is ascending.
Returns:
the object found in the database
Throws:
IllegalArgumentException - if clazz is null or if propreties and values are not of the same length
Since:
jcms-6.0

query

public static <T> List<T> query(Class<? extends T> clazz,
                                Map<String,Object> paramsMap)
Retrieves all object matching the specified parameters.

Example to retrieve ReaderTracker of a publication for a specific member.

   Map<String,String> paramsMap = new HashMap<String,String>();
   paramsMap.put("publicationId", pub.getId());
   paramsMap.put("authorId", mbr.getId());
   HibernateUtil.query(ReaderTracker.class, paramsMap);
 
Results will be sorted by cdate if the Class is a subclass of Data, otherwise, results are not sorted

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required
paramsMap - a map of fields name and field value (if null or empty, no refinement is performed)
Returns:
a list of all object found in the database (never return null)
Since:
jcms-6.0

query

public static <T> List<T> query(Class<? extends T> clazz,
                                Map<String,Object> paramsMap,
                                String orderBy)
Retrieves all object matching the specified parameters.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
paramsMap - a map of fields name and field value (if null or empty, no refinement is performed)
orderBy - the names of the fields to be used to order the results (comma separated). You can append "asc" or "desc" to indicate the order direction. Default is ascending.
Returns:
a list of all object found in the database (never return null)
Throws:
IllegalArgumentException - if clazz is null
Since:
jcms-6.0

query

public static <T> List<T> query(Class<? extends T> clazz,
                                Map<String,Object> paramsMap,
                                String orderBy,
                                int firstResult,
                                int maxResults)
Retrieves all object matching the specified parameters.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
paramsMap - a map of fields name and field value (if null or empty, no refinement is performed)
orderBy - the names of the fields to be used to order the results (comma separated). You can append "asc" or "desc" to indicate the order direction. Default is ascending.
firstResult - set the first data to retrieve. If <= 0, rows will be retrieved beginnning from first data (does not work with query on super classes).
maxResults - set the maximum number of data to retrieve. If < 0, there is no limit to the number of data retrieved (does not work with query on super classes).
Returns:
a list of all object found in the database (never return null)
Throws:
IllegalArgumentException - if clazz is null
Since:
jcms-6.0

handleTimeoutException

public static void handleTimeoutException(Throwable th)
Handle time out exception

Parameters:
th - the throwable
Since:
jcms-6.1.3

buildQueryCriteria

public static org.hibernate.Criteria buildQueryCriteria(org.hibernate.Session session,
                                                        Class<?> clazz,
                                                        String[] properties,
                                                        Object[] values,
                                                        String orderBy,
                                                        int firstResult,
                                                        int maxResults)
Retrieves the result(s) matching the specified parameters.

Returns either a List or an object depending on value of the unique parameter.

Parameters:
session - the Hibernate session to use
clazz - the Class of object to retrieve, required must not be null
properties - an array of fields name. All results are return if null or empty.
values - an array of values to match, must be the same size as properties. All results are return if null or empty.
orderBy - the names of the fields to be used to order the results (comma separated). You can append "asc" or "desc" to indicate the order direction. Default is ascending.
firstResult - set the first data to retrieve. If <= 0, rows will be retrieved beginnning from first data (does not work with query on super classes).
maxResults - set the maximum number of data to retrieve. If < 0, there is no limit to the number of data retrieved (does not work with query on super classes).
Returns:
if unique is false, a List (never return null). if unique is true, an Object (may be null)
Throws:
IllegalArgumentException - if clazz is null or if propreties and values are not of the same length
Since:
jcms-6.0

queryCollection

public static <T> List<T> queryCollection(Class<? extends T> clazz,
                                          String property,
                                          Object value)
Retreives the instance of the given class with the collection property matching the given value.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
property - the collection field name
value - the value to match
Returns:
a list of data
Since:
jcms-6.1.0

queryCollectionRowId

public static <T> List<Long> queryCollectionRowId(Class<? extends T> clazz,
                                                  String property,
                                                  Object value)
Retreives the instance of the given class with the collection property matching the given value.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
property - the collection field name
value - the value to match
Returns:
a list of data
Since:
jcms-6.2.0

scrollCollection

public static <T> org.hibernate.ScrollableResults scrollCollection(Class<? extends T> clazz,
                                                                   String property,
                                                                   Object value,
                                                                   boolean retreiveRowId)
Scroll the instance of the given class with the collection property matching the given value.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
property - the collection field name
value - the value to match
Returns:
a list of data
Since:
jcms-6.2.0

buildQueryCollection

public static <T> org.hibernate.Query buildQueryCollection(Class<? extends T> clazz,
                                                           String property,
                                                           Object value,
                                                           boolean retreiveRowId)
Returns the Hibernate query to search the instance of the given class with the collection property matching the given value.

Type Parameters:
T - Hibernate class corresponding to a table in the database
Parameters:
clazz - the Class of object to retrieve, required must not be null
property - the collection field name
value - the value to match
Returns:
a list of data
Since:
jcms-6.1.0

deleteQuery

public static int deleteQuery(Class<?> clazz,
                              String property,
                              String value)
Delete all object matching the specified parameters.

Example to delete all ReaderTracker of a publication

   HibernateUtil.deleteQuery(ReaderTracker.class, "publicationId", pub.getId());
 

Parameters:
clazz - the Class of object to retrieve and delete, required
property - a field name, required
value - the value of the field to match, required
Returns:
the number of objects deleted.
Since:
jcms-6.0

deleteQuery

public static int deleteQuery(Class<?> clazz,
                              String[] properties,
                              String[] values)
Delete all object matching the specified parameters.

Example to delete all ReaderTracker of a publication when the access count is 1

   HibernateUtil.deleteQuery(ReaderTracker.class, new String[] { "publicationId", "accessCount" }, new String[] { pub.getId(), "1"}, true);
 

Parameters:
clazz - the Class of object to retrieve and delete, required
properties - the fields name, required
values - the value of the field to match, required
Returns:
the number of objects deleted.
Since:
jcms-6.2.0

deleteQuery

public static int deleteQuery(Class<?> clazz,
                              String property,
                              String value,
                              boolean cleanSession)
Delete all object matching the specified parameters.

Example to delete all ReaderTracker of a publication

   HibernateUtil.deleteQuery(ReaderTracker.class, "publicationId", pub.getId());
 

Parameters:
clazz - the Class of object to retrieve and delete, required
property - a field name, required
value - the value of the field to match, required
cleanSession - if true clean the session (i.e. flush/clear). Should be true unless you have others DB access to perform after this call.
Returns:
the number of objects deleted.
Since:
jcms-6.1.0

deleteQuery

public static int deleteQuery(Class<?> clazz,
                              String[] properties,
                              String[] values,
                              boolean cleanSession)
Delete all object matching the specified parameters.

Example to delete all ReaderTracker of a publication when the access count is 1

   HibernateUtil.deleteQuery(ReaderTracker.class, new String[] { "publicationId", "accessCount" }, new String[] { pub.getId(), "1"}, true);
 

Parameters:
clazz - the Class of object to retrieve and delete, required
properties - the fields name, required
values - the value of the field to match, required
cleanSession - if true clean the session (i.e. flush/clear). Should be true unless you have others DB access to perform after this call.
Returns:
the number of objects deleted.
Since:
jcms-6.2.0

getDBDataList

public static <T> List<T> getDBDataList(Class<T> clazz)
Retrieve all Objects of the specified class from the database (limited to maxResults).

Type Parameters:
T - any DB type
Parameters:
clazz - the Class of object to retrieve, required
Returns:
a list of all object found in the database (never return null)
Since:
jcms-6.0

getDBDataList

public static <T> List<T> getDBDataList(Class<T> clazz,
                                        int firstResult,
                                        int maxResults)
Retrieve all Objects of the specified class from the database.

Type Parameters:
T - any DB type
Parameters:
clazz - the Class of object to retrieve, required
firstResult - set the first data to retrieve. If <= 0, rows will be retrieved beginnning from first data (does not work with query on super classes).
maxResults - set the maximum number of data to retrieve. If < 0, there is no limit to the number of data retrieved (does not work with query on super classes).
Returns:
a list of all object found in the database (never return null)
Since:
jcms-6.0

getDBDataList

public static <T> List<T> getDBDataList(Class<T> clazz,
                                        Collection<Long> rowIdColl)
Retrieve a List of Objects from their Class and rowId.

Type Parameters:
T - any DB type
Parameters:
clazz - the Class of object to retrieve, required
rowIdColl - a collection of rowId
Returns:
a list of all object found in the database (never return null)
Since:
jcms-6.1.0

getDBDataIterator

public static <T> Iterator<T> getDBDataIterator(Class<T> clazz)
Retrieve all Objects of the specified class from the database.

Type Parameters:
T - any DB type
Parameters:
clazz - the Class of object to retrieve, required
Returns:
the result iterator
Since:
jcms-6.0

getRowIdBatchSet

public static Set<Set<Long>> getRowIdBatchSet(Collection<String> idColl,
                                              int batchSize)
Convenient method to convert a Collection of id into many smaller collections, allowing batch select using method getDBDataList(Class, Collection).

Example :

  // HibernateUtil.beginTransaction(); already done HibernateSessionFilter for standard HTTP request
 
  // Retrieve DBMember id list from caddy 
  List<String> dbMemberIdList = getCaddy().getDBMemberList();
 
  // Process DBMember in batch
  Set<Set<Long>> batchSet = HibernateUtil.getRowIdBatchSet(dbMemberIdList, -1)
  for (Set<Long> mbrRowIdSet : batchSet) {
    List<DBMember> dbmbrList = HibernateUtil.getDBDataList(DBMember.class, mbrRowIdSet);
    
    for (DBMember dbmbr : dbmbrList) {
      // dbmbr.set...
      HibernateUtil.save(dbmbr);   
    }
    
    // Commit transaction on end of batch
    HibernateUtil.commitTransaction();
    HibernateUtil.beginTransaction();
  }
  
  // HibernateUtil.commitTransaction(); already done HibernateSessionFilter for standard HTTP request
 

Parameters:
idColl - a collection of jcmsid for DBData (e.g 42_Review)
batchSize - the size of subset to be create (defaut value DEFAULT_BATCH_SIZE is used if specified valued is less or equal 1)
Returns:
a Set containing subset of rowId.
Since:
jcms-6.1.0

getBatchSet

public static Set<Set<Long>> getBatchSet(Collection<Long> idColl,
                                         int batchSize)
Convenient method to convert a Collection of rowId into many smaller collections, allowing batch select using method getDBDataList(Class, Collection).

Parameters:
idColl - a collection of rowId for DBData
batchSize - the size of subset to be create (defaut value DEFAULT_BATCH_SIZE is used if specified valued is less or equal 1)
Returns:
a Set containing subset of rowId.
Since:
jcms-7.0.2
See Also:
getRowIdBatchSet(Collection, int)

queryCount

public static int queryCount(Class<?> clazz)
Returns the count of objects matching the given class.

Parameters:
clazz - the class
Returns:
the count of objects matching the given class.
Since:
jcms-6.0.0

queryCount

public static int queryCount(Class<?> clazz,
                             String property,
                             Object value)
Returns the count of objects matching the given class with the given criteria.

Parameters:
clazz - the class
property - a field name.
value - the value of the field to match. All results are return if null.
Returns:
the count of objects matching the given class with the given criteria.
Since:
jcms-6.0.0

queryCount

public static int queryCount(Class<?> clazz,
                             String[] properties,
                             Object[] values)
Returns the count of objects matching the given class with the given criteria.

Parameters:
clazz - the class
properties - an array of field name.
values - an array of values of the field to match.
Returns:
the count of objects matching the given class with the given criteria.
Since:
jcms-6.0.0

queryCount

public static int queryCount(Class<?> clazz,
                             org.hibernate.criterion.Criterion criterion)
Returns the count of objects matching the given class with the given criterion.

Parameters:
clazz - the class
criterion - the criterion
Returns:
the count of objects matching the given class with the given criterion.
Since:
jcms-6.0.0

queryCount

public static int queryCount(org.hibernate.Criteria criteria)
Returns the count of objects matching the given criteria.

Parameters:
criteria - the criteria
Returns:
the count of objects matching the given criteria.
Since:
jcms-6.2.0

queryPublication

public static PageResult<Publication> queryPublication(PublicationCriteria pubCriteria)
Returns a list of publications in the database matching the given criteria.

Parameters:
pubCriteria - the PublicationCriteria (required, must not be null)
Returns:
a list of publications, never return null.
Throws:
IllegalArgumentException - if pubCriteria is null
Since:
jcms-6.0.0

filterSearchResultByIdList

public static LinkedHashMap<String,Float> filterSearchResultByIdList(LinkedHashMap<String,Float> resultMap,
                                                                     List<String> idList)
Filters the lucene search result with the given list of publication's identifier.

Parameters:
resultMap - a given map of JCMS id with score associated.
idList - the list of publication's identifier
Returns:
a map of publication's matching the lucene query and their score. This map is a subset of idList and respect its order.
Since:
jcms-6.1.2

buildAndCriterion

public static org.hibernate.criterion.Criterion buildAndCriterion(org.hibernate.criterion.Criterion c1,
                                                                  org.hibernate.criterion.Criterion c2)
Returns the AND of the two criterion. Check if criterion are null.

Parameters:
c1 - a criterion
c2 - a criterion
Returns:
the AND of the two criterion. Check if criterion are null.
Since:
jcms-6.0.0

buildOrCriterion

public static org.hibernate.criterion.Criterion buildOrCriterion(org.hibernate.criterion.Criterion c1,
                                                                 org.hibernate.criterion.Criterion c2)
Returns the OR of the two criterion. Check if criterion are null.

Parameters:
c1 - a criterion
c2 - a criterion
Returns:
the OR of the two criterion. Check if criterion are null.
Since:
jcms-6.0.0

buildCollectionCriterion

public static org.hibernate.criterion.Criterion buildCollectionCriterion(Class clazz,
                                                                         String property,
                                                                         String value)
Apply a constraint on a value of the collection of the given clazz.

Parameters:
clazz - the entity class
property - the name of the collection property
value - the searched value
Returns:
a SQL restriction with the given parameters.
Since:
jcms-6.1.0

buildCollectionCriterion

public static org.hibernate.criterion.Criterion buildCollectionCriterion(Class clazz,
                                                                         String property,
                                                                         Object value,
                                                                         org.hibernate.type.Type valueType)
Apply a constraint on a value of the collection of the given clazz. Warning! This method does not support polymorphism. The given clazz must be an Hibernate mapped class.

Parameters:
clazz - the entity class. This class must be an Hibernate mapped class.
property - the name of the collection property
value - the searched value
valueType - the type of the value (e.g. Hibernate.STRING)
Returns:
a SQL restriction with the given parameters.
Since:
jcms-6.1.0

buildCollectionCriterion

public static org.hibernate.criterion.Criterion buildCollectionCriterion(String entityTable,
                                                                         String idColumn,
                                                                         String collectionTable,
                                                                         String itemIdColumn,
                                                                         String valueColumn,
                                                                         Object value,
                                                                         org.hibernate.type.Type valueType)
Apply a constraint on a value of the collection of the given entity.

Parameters:
entityTable - the entity table name
idColumn - the ID column name of the entity table
collectionTable - the collection table name
itemIdColumn - the ID column name of the collection table
valueColumn - the name of the value column
value - the searched value
valueType - the type of the value (e.g. Hibernate.STRING)
Returns:
a SQL restriction with the given parameters.
Since:
jcms-6.1.0

checkSQLInjection

public static String checkSQLInjection(String... strings)
Checks if the given strings may contains an SQL injection.

Parameters:
strings - an array of String
Returns:
the String which contains a possible SQL injection or null if no SQL injection has been detected.
Since:
jcms-6.1.0

getObject

public static Object getObject(Class clazz,
                               Long rowId)
Retrieves the Object of the specified class and using the specified id.

Parameters:
clazz - the Class (or super class in case of inheritance) of the object to retrieve
rowId - the primary key value of the object or if could not be found, the value of the "id" column
Returns:
the Object found in the database or null if no object matched the query.
Since:
jcms-6.0

getObject

public static Object getObject(org.hibernate.Session session,
                               Class clazz,
                               Long rowId)
Retrieves the Object of the specified class and using the specified id in the given session.

Parameters:
session - the session, must not be null (otherwise, null is returned)
clazz - the Class (or super class in case of inheritance) of the object to retrieve, must not be null
rowId - the primary key value of the object or if could not be found, the value of the "id" column, must not be null
Returns:
the Object found in the database or null if no object matched the query or if any parameter were null.
Since:
jcms-6.2.0

getDBData

public static DBData getDBData(String id)
Retrieves the DBData bound to the specified jcms id from the database.

Parameters:
id - the JCMS id of this data (e.g. 1234_WFNOTE)
Returns:
the DBData found in the database or null if no object matched the query.
Since:
jcms-6.0

parseJcmsId

public static Object[] parseJcmsId(String id)
Returns the 2 component of the given JCMS ID.

Parameters:
id - the JCMS id
Returns:
an array of 2 components : the rowId (Long) and the class (Class)
Since:
jcms-6.2.0

getRowId

public static Long getRowId(String id)
Retrieve the row id from a DBData id

Parameters:
id - the full DBData id as returned by dbdata.getId() (e.g 42_Review)
Returns:
the rowId computed or null if it could not parsed
Since:
jcms-6.1.0

getData

public static Data getData(String id)
Retrieves the Data bound to the specified jcms id from the database.

Parameters:
id - the JCMS id of this data (e.g. 1234_WFNOTE)
Returns:
the Data found in the database or null if no object matched the query.
Since:
jcms-6.0.0

getCurrentObject

public static Object getCurrentObject(Class clazz,
                                      Long rowId)
Return the current persisted state of the object with the given rowId.

Parameters:
clazz - the class of the object
rowId - the rowId of the object
Returns:
the current persisted state of the object with the given id.
Since:
jcms-6.2.0

getCurrentDBData

public static DBData getCurrentDBData(String id)
Return the current persisted state of the DBData with the given id.

Parameters:
id - the JCMS id
Returns:
the current persisted state of the DBData with the given id.
Since:
jcms-6.2.0

getCurrentData

public static Data getCurrentData(String id)
Return the current persisted state of the Data with the given id.

Parameters:
id - the JCMS id
Returns:
the current persisted state of the Data with the given id.
Since:
jcms-6.2.0

save

public static void save(Data data)
Saves or updates the specified Data in the database.

Parameters:
data - the Data to be saved or updated
Since:
jcms-6.0

save

public static void save(Object obj)
Saves or updates the specified Object in the database.

Parameters:
obj - the object to be saved or updated
Since:
jcms-6.0

supportDateMillis

public static boolean supportDateMillis()
Returns true if the database supports millis for date.

Returns:
true if the database supports millis for date.
Since:
jcms-6.2.0

supportFullJoin

public static boolean supportFullJoin()
Returns true if the database supports full join.

Returns:
true if the database supports full join.
Since:
jcms-7.0.1

getCurrentDate

public static Date getCurrentDate()
Returns a new date. The millis are truncated or not according the database support of millis.

Returns:
a new date.
Since:
jcms-6.2.0

evict

public static void evict(Object obj)
Removes this instance from the Hibernate session cache to avoid automatic persistence by dirty checking.

Parameters:
obj -
Since:
jcms-6.0.0
See Also:
Session.evict(Object)

delete

public static void delete(DBData data,
                          boolean cleanSession)
Deletes the specified object in the database.

Parameters:
data - the data to be deleted
cleanSession - if true clean the session
Since:
jcms-6.1.1

delete

public static void delete(DBData data)
Deletes the specified object in the database.

Parameters:
data - the data to be deleted
Since:
jcms-6.0

delete

public static void delete(String id)
Deletes the specified object in the database.

Parameters:
id - the identifier of the DBData to be deleted
Since:
jcms-6.0

clearCache

public static void clearCache()

removeSecondLevelCacheEntry

public static void removeSecondLevelCacheEntry(String entityName,
                                               Long rowId)
Removes an DBData from the second level cache.

Synchronization is handled within the method.

Parameters:
entityName - the id of the DBData to remove in the second level cache.
Throws:
IllegalStateException - if the cache is not Status.STATUS_ALIVE
Since:
jcms-7.0.0

scroll

public static org.hibernate.ScrollableResults scroll(Class clazz)
Scroll over a class. Scroll in forward only.

Parameters:
clazz - the class to scroll over.
Returns:
a ScrollableResults
Since:
jcms-6.1.0

scroll

public static org.hibernate.ScrollableResults scroll(Class clazz,
                                                     org.hibernate.ScrollMode scrollMode)
Scroll over a class with a given scroll mode.

Parameters:
clazz - the class to scroll over.
scrollMode - Indicates the type of underlying database cursor to request.
Returns:
a ScrollableResults
Since:
jcms-6.2.0


Copyright © 2001-2010 Jalios SA. All Rights Reserved.