org.rhq.core.server
Class PersistenceUtility

java.lang.Object
  extended by org.rhq.core.server.PersistenceUtility

public class PersistenceUtility
extends Object

Various persistence utility methods - mostly Hibernate-specific.


Field Summary
static String HIBERNATE_STATISTICS_MBEAN_OBJECTNAME
           
 
Constructor Summary
PersistenceUtility()
           
 
Method Summary
static String addH2NativePagingSortingToQuery(String query, PageControl pageControl)
           
static String addOracleNativePagingSortingToQuery(String query, PageControl pageControl)
           
static String addPostgresNativePagingSortingToQuery(String query, PageControl pageControl)
           
static String addSQLServerNativePagingSortingToQuery(String query, PageControl pageControl)
          Note: always put the rownum column at the END of the columns, so that code which relies on index-based access to the result set data doesn't break Method 1: SELECT outerResults.* FROM ( SELECT innerResults.*, ROW_NUMBER() OVER( {orderByClause} ) AS rownum FROM ( {queryWithoutOrderBy} ) AS innerResults ) AS outerResults WHERE rownum <= maxRowNum AND rownum >= minRowNum The above method fails in circumstances where the orderByClause is built up with aliases that aren't in the explicit select list returned from queryWithoutOrderBy Method 2: Fix above shortcomings by pushing the orderByClause into the actual select list SELECT singleResults.* FROM ( {queryWithoutOrderBySelectList} , ROW_NUMBER() OVER( {orderByClause} ) AS rownum {queryWithoutOrderByRestOfQuery} ) AS singleResults WHERE rownum <= maxRowNum AND rownum >= minRowNum Actually, both of the above methods have small flaws.
static String addSQLServerNativePagingSortingToQuery(String query, PageControl pageControl, boolean alternatePagingStyle)
           
static Object cast(String value, org.hibernate.type.Type hibernateType)
           
static Query createCountQuery(EntityManager em, String queryName)
          Builds a count(*) version of the named query so we don't have duplicate all our queries to use two query pagination model.
static Query createCountQuery(EntityManager entityManager, String queryName, String countItem)
          Builds a count(*) version of the named query so we don't have duplicate all our queries to use two query pagination model.
static PageList createPaginationFilter(EntityManager entityManager, Collection collection, PageControl pageControl)
          Creates and executes a filter query for a collection relationship.
static Query createQueryWithOrderBy(EntityManager entityManager, String queryName, OrderingField... orderByFields)
          Create a query from a named query with a transformed order by clause with multiple new ordery by clauses.
static Query createQueryWithOrderBy(EntityManager entityManager, String queryName, PageControl pageControl)
          Used to create queries to use with the PageControl objects.
static void enableHibernateStatistics(EntityManager entityManager, MBeanServer server)
          Enables the hibernate statistics mbean to provide access to information on the ejb3 persistence tier.
static
<T> List<T>
findByCriteria(EntityManager entityManager, Class<T> type, org.hibernate.criterion.Criterion... criterion)
          Use this inside subclasses as a convenience method.
static String getDisplayString(org.hibernate.type.Type hibernateType)
           
static org.hibernate.Session getHibernateSession(EntityManager entityManager)
           
static org.hibernate.stat.Statistics getStatisticsService(EntityManager entityManager, MBeanServer server)
           
static void setDataPage(Query query, PageControl pageControl)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HIBERNATE_STATISTICS_MBEAN_OBJECTNAME

public static final String HIBERNATE_STATISTICS_MBEAN_OBJECTNAME
See Also:
Constant Field Values
Constructor Detail

PersistenceUtility

public PersistenceUtility()
Method Detail

getDisplayString

public static String getDisplayString(org.hibernate.type.Type hibernateType)

cast

public static Object cast(String value,
                          org.hibernate.type.Type hibernateType)

createQueryWithOrderBy

public static Query createQueryWithOrderBy(EntityManager entityManager,
                                           String queryName,
                                           PageControl pageControl)
Used to create queries to use with the PageControl objects. The query will already have its sort column and order appended as well as having its first result and max results set according to the page control data.

Parameters:
entityManager - your entity manager
queryName - name of the query
pageControl - the controls on the paging and sorting
Returns:
a preconfigured query for ordered pagination

createQueryWithOrderBy

public static Query createQueryWithOrderBy(EntityManager entityManager,
                                           String queryName,
                                           OrderingField... orderByFields)
Create a query from a named query with a transformed order by clause with multiple new ordery by clauses.

Parameters:
entityManager - the entity manager to build the query against
queryName - the name of the query to transform
orderByFields - an array of clauses to contribute to the order by
Returns:
the transformed query

createCountQuery

public static Query createCountQuery(EntityManager em,
                                     String queryName)
Builds a count(*) version of the named query so we don't have duplicate all our queries to use two query pagination model.

Parameters:
em - the entity manager to build the query for
queryName - the NamedQuery to transform
Returns:
a query that can be bound and executed to get the total count of results

createCountQuery

public static Query createCountQuery(EntityManager entityManager,
                                     String queryName,
                                     String countItem)
Builds a count(*) version of the named query so we don't have duplicate all our queries to use two query pagination model.

Parameters:
entityManager - the entity manager to build the query for
queryName - the NamedQuery to transform
countItem - the object or attribute that needs to be counted, when it's ambiguous
Returns:
a query that can be bound and executed to get the total count of results

setDataPage

public static void setDataPage(Query query,
                               PageControl pageControl)

createPaginationFilter

public static PageList createPaginationFilter(EntityManager entityManager,
                                              Collection collection,
                                              PageControl pageControl)
Creates and executes a filter query for a collection relationship. This executes without passing back the query object because the most common case is to simply paginate for a relationship. Use the createFilter method to create more generic filters and get access to the hibernate query object for setting parameters etc.

Parameters:
entityManager -
collection -
pageControl -
Returns:
the result list of the entities from the filtered relationship

findByCriteria

public static <T> List<T> findByCriteria(EntityManager entityManager,
                                         Class<T> type,
                                         org.hibernate.criterion.Criterion... criterion)
Use this inside subclasses as a convenience method.


getHibernateSession

public static org.hibernate.Session getHibernateSession(EntityManager entityManager)

enableHibernateStatistics

public static void enableHibernateStatistics(EntityManager entityManager,
                                             MBeanServer server)
Enables the hibernate statistics mbean to provide access to information on the ejb3 persistence tier.

Parameters:
entityManager - an inject entity manager whose session factory will be tracked with these statistics
server - the MBeanServer where the statistics MBean should be registered; if null, the first one in the list returned by MBeanServerFactory.findMBeanServer(null) is used

getStatisticsService

public static org.hibernate.stat.Statistics getStatisticsService(EntityManager entityManager,
                                                                 MBeanServer server)

addPostgresNativePagingSortingToQuery

public static String addPostgresNativePagingSortingToQuery(String query,
                                                           PageControl pageControl)

addOracleNativePagingSortingToQuery

public static String addOracleNativePagingSortingToQuery(String query,
                                                         PageControl pageControl)

addSQLServerNativePagingSortingToQuery

public static String addSQLServerNativePagingSortingToQuery(String query,
                                                            PageControl pageControl)
Note: always put the rownum column at the END of the columns, so that code which relies on index-based access to the result set data doesn't break Method 1: SELECT outerResults.* FROM ( SELECT innerResults.*, ROW_NUMBER() OVER( {orderByClause} ) AS rownum FROM ( {queryWithoutOrderBy} ) AS innerResults ) AS outerResults WHERE rownum <= maxRowNum AND rownum >= minRowNum The above method fails in circumstances where the orderByClause is built up with aliases that aren't in the explicit select list returned from queryWithoutOrderBy Method 2: Fix above shortcomings by pushing the orderByClause into the actual select list SELECT singleResults.* FROM ( {queryWithoutOrderBySelectList} , ROW_NUMBER() OVER( {orderByClause} ) AS rownum {queryWithoutOrderByRestOfQuery} ) AS singleResults WHERE rownum <= maxRowNum AND rownum >= minRowNum Actually, both of the above methods have small flaws. The first can not sort by columns that aren't in the explicit return list. The second can not sort by computed columns and subqueries in the select list. The only way I see how this can work is by modifying the queryWithoutOrderBy to explicitly return all parameters that will be sorted on (even if the use case wouldn't normally require them to be in the select list), alias them, and order by the aliases by modifying the web ui code to use those tokens when generating the sortable column headers (jmarques - June/2009)


addSQLServerNativePagingSortingToQuery

public static String addSQLServerNativePagingSortingToQuery(String query,
                                                            PageControl pageControl,
                                                            boolean alternatePagingStyle)

addH2NativePagingSortingToQuery

public static String addH2NativePagingSortingToQuery(String query,
                                                     PageControl pageControl)


Copyright © 2008-2011 Red Hat, Inc.. All Rights Reserved.