查询HQL日志
parent
491de16d37
commit
5b5d072423
|
|
@ -271,13 +271,9 @@ public class QueryAction extends AbstractAction implements ApplicationContextAwa
|
|||
|
||||
for (Object key : params.keySet()) {
|
||||
Object param=params.get(key);
|
||||
|
||||
String param_str=param.toString();
|
||||
//log.info(key.toString()+"====="+param_str+"--"+param_str.indexOf("%"));
|
||||
if(param_str.indexOf("%")==0){
|
||||
//log.info("替换开始");
|
||||
params.put(key, param_str.substring(1));
|
||||
|
||||
log.info("替换------"+key.toString()+"====="+param_str+"--"+param_str.indexOf("%"));
|
||||
}
|
||||
|
||||
|
|
@ -306,14 +302,11 @@ public class QueryAction extends AbstractAction implements ApplicationContextAwa
|
|||
} else if (tableConfig.getSortHql() != null && !"".equals(tableConfig.getSortHql())) {
|
||||
hql3 = String.valueOf(hql3.substring(0, index)) + tableConfig.getSortHql() + " ," + hql3.substring(index + 8, hql3.length());
|
||||
}
|
||||
//log.info("------------------------------------------------------------------------------------------------查询方式1------------------------------------------------------------------------------------------------ ");
|
||||
result = buildHqlGrid(tableConfig, hql3, params);
|
||||
} else if (tableConfig.getDataSourceType().equals(DataSource.JAVA_TYPE)) {
|
||||
//log.info("------------------------------------------------------------------------------------------------查询方式2------------------------------------------------------------------------------------------------ ");
|
||||
result = buildJavaGrid(tableConfig, hql3, params);
|
||||
}
|
||||
params.clear();
|
||||
// log.info("------------------------------------------------------------------------------------------------查询结束------------------------------------------------------------------------------------------------ ");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,866 @@
|
|||
package com.dev.energy.server.valuelist.adapter.hib3;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import net.mlw.vlh.DefaultListBackedValueList;
|
||||
import net.mlw.vlh.ValueList;
|
||||
import net.mlw.vlh.ValueListInfo;
|
||||
import net.mlw.vlh.adapter.AbstractValueListAdapter;
|
||||
import net.mlw.vlh.adapter.util.ObjectValidator;
|
||||
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.hibernate.CacheMode;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.ScrollableResults;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.orm.hibernate3.SessionFactoryUtils;
|
||||
|
||||
import com.dev.energy.server.model.DomainModel;
|
||||
import com.dev.energy.server.util.HqlCountParserSupport;
|
||||
import com.dev.energy.server.valuelist.adapter.hib3.util.ScrollableResultsDecorator;
|
||||
import com.dev.energy.server.valuelist.adapter.hib3.util.StatementBuilder;
|
||||
|
||||
/**
|
||||
* This adapter wraps the functionality of Hibernate.
|
||||
* Add extra functionality such as paging, focusing
|
||||
* and validating of current result set.
|
||||
* <i>
|
||||
* "Hibernate is a powerful, ultra-high performance
|
||||
* object/relational persistence and query service
|
||||
* for Java. Hibernate lets you develop persistent
|
||||
* classes following common Java idiom - including
|
||||
* association, inheritance, polymorphism, composition
|
||||
* and the Java collections framework. The Hibernate
|
||||
* Query Language, designed as a "minimal"
|
||||
* object-oriented extension to SQL, provides an
|
||||
* elegant bridge between the object and relational
|
||||
* worlds. Hibernate also allows you to express queries
|
||||
* using native SQL or Java-based Criteria and Example
|
||||
* queries. Hibernate is now the most popular
|
||||
* object/relational mapping solution for Java."
|
||||
* </i> -http://www.hibernate.org/
|
||||
*
|
||||
* @author dev
|
||||
* @version $Revision: 1.1.1.1 $ $Date: 2010/06/29 01:59:03 $
|
||||
*
|
||||
* @note Updated by Olexiy Prokhorenko (olexiy@objecty.com) for Hibernate 3
|
||||
*/
|
||||
public class Hibernate30Adapter extends AbstractValueListAdapter implements ValueListAdapter
|
||||
{
|
||||
/** The Hibernate SessionFactory. */
|
||||
private SessionFactory sessionFactory;
|
||||
private String cacheRegion;
|
||||
/**
|
||||
* <p>
|
||||
* If is set, it use special ScrollableResultsDecorator, that enable or
|
||||
* disable to add object in final list.
|
||||
* </p>
|
||||
* <h4>NOTE:</h4>
|
||||
* <p>
|
||||
* Also, it respects the total count of entries that overlap your paged
|
||||
* list.
|
||||
* </p>
|
||||
*/
|
||||
private ObjectValidator _validator = null;
|
||||
|
||||
/** Commons logger. */
|
||||
protected static final Log LOGGER = LogFactory
|
||||
.getLog(Hibernate30Adapter.class);
|
||||
|
||||
/** If a new Session should be created if no thread-bound found. */
|
||||
private boolean allowCreate = true;
|
||||
|
||||
/** The hibernate query. */
|
||||
private String hql;
|
||||
|
||||
private String namedQuery;
|
||||
|
||||
/** The max rows in ResulSet to doFocus
|
||||
* @author dev
|
||||
*/
|
||||
private long maxRowsForFocus = Long.MAX_VALUE;
|
||||
|
||||
/** The name of object use to get focus property in hibernate sql syntax
|
||||
* SELECT defaultFocusPropertyObjectAlias.getFocusProperty ...
|
||||
*
|
||||
* @author dev
|
||||
*/
|
||||
private String defaultFocusPropertyObjectAlias = "";
|
||||
|
||||
/**
|
||||
* Enable or Disable String length checking of given filters values. If
|
||||
* filter value is null or empty is removed from query.
|
||||
* @author dev
|
||||
*/
|
||||
private boolean _isRemoveEmptyStrings = false;
|
||||
|
||||
private StatementBuilder statementBuilder;
|
||||
|
||||
/**
|
||||
* Enable or disable optimalization of the query for focus property.
|
||||
*/
|
||||
private boolean _focusOptimalization = true;
|
||||
|
||||
/**
|
||||
* @return Returns the focusOptimalization.
|
||||
*/
|
||||
public boolean isFocusOptimalization()
|
||||
{
|
||||
return _focusOptimalization;
|
||||
}
|
||||
/**
|
||||
* Enable or disable optimalization of the query for focus property.
|
||||
* @param focusOptimalization true - enable query with short select, false - query with full select
|
||||
*/
|
||||
public void setFocusOptimalization(boolean focusOptimalization)
|
||||
{
|
||||
_focusOptimalization = focusOptimalization;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* If is set, it use special ScrollableResultsDecorator, that enable or
|
||||
* disable to add object in final list.
|
||||
* </p>
|
||||
* <h4>NOTE:</h4>
|
||||
* <p>
|
||||
* Also, it respects the total count of entries that overlap your paged
|
||||
* list.
|
||||
* </p>
|
||||
* @param validator The validator to set.
|
||||
*/
|
||||
public void setValidator(ObjectValidator validator)
|
||||
{
|
||||
_validator = validator;
|
||||
}
|
||||
/**
|
||||
* @return Returns the isPrefilterEmpty.
|
||||
*/
|
||||
public boolean isRemoveEmptyStrings()
|
||||
{
|
||||
return _isRemoveEmptyStrings;
|
||||
}
|
||||
/**
|
||||
* Enable or Disable String length checking of given filters values. If
|
||||
* filter value is null or empty is removed from query.
|
||||
*
|
||||
* @param isPrefilterEmpty
|
||||
* true-remove null and empty, false - remove only null filters.
|
||||
*/
|
||||
public void setRemoveEmptyStrings(boolean isPrefilterEmpty)
|
||||
{
|
||||
_isRemoveEmptyStrings = isPrefilterEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see net.mlw.vlh.ValueListAdapter#getValueList(java.lang.String,
|
||||
* net.mlw.vlh.ValueListInfo)
|
||||
*/
|
||||
public ValueList getValueList(String hql, ValueListInfo info)
|
||||
{
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("getValueList(String, ValueListInfo) - start");
|
||||
}
|
||||
|
||||
if (info.getSortingColumn() == null)
|
||||
{
|
||||
info.setPrimarySortColumn(getDefaultSortColumn());
|
||||
info.setPrimarySortDirection(getDefaultSortDirectionInteger());
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("The default sort column '" + getDefaultSortColumn() + "' with direction '"+getDefaultSortDirectionInteger()+"' was set.");
|
||||
}
|
||||
}
|
||||
|
||||
if (info.getPagingNumberPer() == Integer.MAX_VALUE)
|
||||
{
|
||||
info.setPagingNumberPer(getDefaultNumberPerPage());
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("The paging number per page '" + getDefaultNumberPerPage() + "' was set.");
|
||||
}
|
||||
}
|
||||
|
||||
Session session = SessionFactoryUtils.getSession(getSessionFactory(),allowCreate);
|
||||
try
|
||||
{
|
||||
Query query;
|
||||
|
||||
boolean doFocus = ((getAdapterType() & DO_FOCUS) == 0)
|
||||
&& info.isFocusEnabled() && info.isDoFocus()
|
||||
&& (namedQuery == null);
|
||||
|
||||
if (doFocus)
|
||||
{
|
||||
ScrollableResults results = getScrollableResults(getQueryForFocus(info, session), info);
|
||||
results.beforeFirst();
|
||||
doFocusFor(info, results);
|
||||
}
|
||||
if(LOGGER.isDebugEnabled()){
|
||||
LOGGER.debug("Query hql = " + hql);
|
||||
}
|
||||
LOGGER.info("-------------------------------开始内部SQL-----------------------------------");
|
||||
query = getQuery(hql,info, session);
|
||||
if(cacheRegion != null){
|
||||
query.setCacheable(true);
|
||||
query.setCacheRegion(cacheRegion);
|
||||
query.setCacheMode(CacheMode.NORMAL);
|
||||
}
|
||||
|
||||
boolean doPage = ((getAdapterType() & DO_PAGE) == 0);
|
||||
List list;
|
||||
list = new ArrayList(info.getPagingNumberPer());
|
||||
if (doPage)
|
||||
{
|
||||
int numberPerPage = (info.getPagingNumberPer() > 0) ? info.getPagingNumberPer() : getDefaultNumberPerPage();
|
||||
int start = (info.getPagingPage() - 1) * numberPerPage;
|
||||
query.setFirstResult(start);
|
||||
query.setFetchSize(numberPerPage);
|
||||
query.setMaxResults(numberPerPage);
|
||||
Long st_Time=System.currentTimeMillis();
|
||||
List results = query.list();
|
||||
Long ed_Time=System.currentTimeMillis();
|
||||
LOGGER.info("查询数据用时:"+(ed_Time-st_Time));
|
||||
for (int i = 0; i < results.size(); i++)
|
||||
{
|
||||
if(i == results.size()){
|
||||
break;
|
||||
}
|
||||
Object result = results.get(i);
|
||||
if (result instanceof Object[]) {
|
||||
Object[] objs = (Object[]) result;
|
||||
if(objs.length == 1 && objs[0] instanceof DomainModel){
|
||||
list.add(objs[0]);
|
||||
}
|
||||
else{
|
||||
list.add(result);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(result instanceof DomainModel){
|
||||
list.add(result);
|
||||
}
|
||||
else{
|
||||
list.add(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
ed_Time=System.currentTimeMillis();
|
||||
LOGGER.info("处理数据用时:"+(ed_Time-st_Time));
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("Sorting finished.");
|
||||
}
|
||||
|
||||
String countHql = HqlCountParserSupport.getCountHql(hql);
|
||||
int si = -1 , j;
|
||||
si = countHql.toLowerCase().lastIndexOf("/~sortcolumn:");
|
||||
if (si > 0) {
|
||||
j = countHql.toLowerCase().indexOf("~/" , si);
|
||||
if (j > 0) {
|
||||
int k = countHql.indexOf(")" , j);
|
||||
if (k < 0) {
|
||||
countHql = countHql.substring(0 , si) + countHql.substring(j + 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
si = countHql.toLowerCase().lastIndexOf("order by");
|
||||
if (si > 0) {
|
||||
int k = countHql.indexOf(")" , si);
|
||||
if (k < 0) {
|
||||
countHql = countHql.substring(0 , si);
|
||||
}
|
||||
}
|
||||
si = countHql.toLowerCase().lastIndexOf("group by");
|
||||
if (si > 0) {
|
||||
int k = countHql.indexOf(")" , si);
|
||||
if (k < 0) {
|
||||
countHql = countHql.substring(0 , si);
|
||||
}
|
||||
}
|
||||
if(LOGGER.isDebugEnabled()){
|
||||
LOGGER.debug("Count hql = " + countHql);
|
||||
}
|
||||
ed_Time=System.currentTimeMillis();
|
||||
LOGGER.info("生成总条数用时:"+(ed_Time-st_Time));
|
||||
Query countQuery = getStatementBuilder().generate(session, new StringBuffer(countHql), info.getFilters(), _isRemoveEmptyStrings);
|
||||
List result = countQuery.list();
|
||||
ed_Time=System.currentTimeMillis();
|
||||
LOGGER.info("查询总条数用时:"+(ed_Time-st_Time));
|
||||
if (result.size() > 0){
|
||||
int lastRowNumber = Integer.parseInt(result.get(0).toString());//((Integer) result.get(0)).intValue();
|
||||
info.setTotalNumberOfEntries(lastRowNumber);
|
||||
}else{
|
||||
info.setTotalNumberOfEntries(0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("Retrieving a list directly from the query.");
|
||||
}
|
||||
list = query.list();
|
||||
info.setTotalNumberOfEntries(list.size());
|
||||
}
|
||||
ValueList returnValueList = getListBackedValueList(info, list);
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("Retrieved list was wrapped in valuelist, info=" + info);
|
||||
}
|
||||
return returnValueList;
|
||||
}
|
||||
catch (HibernateException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
throw SessionFactoryUtils.convertHibernateAccessException(e);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
//SessionFactoryUtils.closeSessionIfNecessary(session, getSessionFactory());
|
||||
SessionFactoryUtils.releaseSession(session, getSessionFactory());
|
||||
}
|
||||
}
|
||||
|
||||
private String getIdExp(int selectIdx, int fromIdx, String origenHql) {
|
||||
int dotIdIndex;//.id 位置
|
||||
int nearestSpaceToId;//离.id最近的空格
|
||||
dotIdIndex = origenHql.toLowerCase().indexOf(".id ", selectIdx);
|
||||
if(dotIdIndex == -1 || dotIdIndex > fromIdx) {
|
||||
dotIdIndex = origenHql.toLowerCase().indexOf(".id,", selectIdx);
|
||||
if(dotIdIndex == -1){
|
||||
return "";
|
||||
}
|
||||
}
|
||||
String temp = origenHql.substring(0, dotIdIndex);
|
||||
nearestSpaceToId = temp.toLowerCase().lastIndexOf(" ");
|
||||
if(nearestSpaceToId == -1) {
|
||||
return "";
|
||||
}
|
||||
return origenHql.substring(nearestSpaceToId, dotIdIndex + 3);
|
||||
}
|
||||
|
||||
private String whiteSpaceSplitHql(String hql) {
|
||||
String[] array = StringUtils.split(hql);
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for(String token : array){
|
||||
buffer.append(token);
|
||||
buffer.append(" ");
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
/**
|
||||
* @param info
|
||||
* @param list
|
||||
* @return DefaultListBackValueList instance
|
||||
*/
|
||||
protected ValueList getListBackedValueList(ValueListInfo info, List list) {
|
||||
return new DefaultListBackedValueList(list,info);
|
||||
}
|
||||
/**
|
||||
* @param info
|
||||
* @param results
|
||||
* @throws HibernateException
|
||||
* @throws NoSuchMethodException
|
||||
* @throws InvocationTargetException
|
||||
* @throws IllegalAccessException
|
||||
*/
|
||||
private void doFocusFor(ValueListInfo info, ScrollableResults results)
|
||||
throws HibernateException
|
||||
{
|
||||
info.setFocusStatus(ValueListInfo.FOCUS_NOT_FOUND);
|
||||
|
||||
int currentRow;
|
||||
if (isFocusOptimalization() == true)
|
||||
{
|
||||
LOGGER.debug("Focusing only property '" + info.getFocusProperty() + "' == '" +info.getFocusValue()+"'." );
|
||||
for (currentRow = 0; ((results.next()) && (currentRow < maxRowsForFocus)); currentRow++)
|
||||
{
|
||||
String value = results.get(0).toString();
|
||||
if (value.equalsIgnoreCase(info.getFocusValue()))
|
||||
{
|
||||
LOGGER.info("Focus property '" + info.getFocusProperty() + "' in row '" + currentRow +"'." );
|
||||
info.setPagingPageFromRowNumber(results.getRowNumber());
|
||||
info.setFocusedRowNumberInTable(results.getRowNumber());
|
||||
info.setFocusStatus(ValueListInfo.FOCUS_FOUND);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.debug("Focusing object with the property '" + info.getFocusProperty() + "' == '" +info.getFocusValue()+"'." );
|
||||
for (currentRow = 0; ((results.next()) && (currentRow < maxRowsForFocus)); currentRow++)
|
||||
{
|
||||
|
||||
Object value;
|
||||
try
|
||||
{
|
||||
value = PropertyUtils.getProperty(results.get(0), info
|
||||
.getFocusProperty());
|
||||
}
|
||||
catch (HibernateException e)
|
||||
{
|
||||
LOGGER.error("Error getting focus property '" + info.getFocusProperty() + "'",e);
|
||||
throw e;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.warn("Ingoring error while getting focus property '" + info.getFocusProperty() + "'",e);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (value.toString().equalsIgnoreCase(info.getFocusValue()))
|
||||
{
|
||||
LOGGER.info("Focus object's property '" + info.getFocusProperty() + "' was found in the row '" + currentRow +"'." );
|
||||
info.setPagingPageFromRowNumber(results.getRowNumber());
|
||||
info.setFocusedRowNumberInTable(results.getRowNumber());
|
||||
info.setFocusStatus(ValueListInfo.FOCUS_FOUND);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( currentRow == maxRowsForFocus)
|
||||
{
|
||||
LOGGER.info("Focus for property '" + info.getFocusProperty() + "' exceded maximum rows for focus '" + maxRowsForFocus +"'." );
|
||||
info.setFocusStatus(ValueListInfo.FOCUS_TOO_MANY_ITEMS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param query
|
||||
* @param info ValueListInfo This info will be set to validator.
|
||||
* @return ScrollableResults, if is set non null _validator, it returns the
|
||||
* ScrollableResultsDecorator.
|
||||
* @throws HibernateException
|
||||
*/
|
||||
private ScrollableResults getScrollableResults(Query query, ValueListInfo info)
|
||||
throws HibernateException
|
||||
{
|
||||
ScrollableResults results;
|
||||
|
||||
if (_validator == null)
|
||||
{
|
||||
LOGGER.debug("Validator is null, using normal ScrollableResults");
|
||||
results = query.scroll();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.info("Using decorator of the ScrollableResults with your validator.");
|
||||
_validator.setValueListInfo(info);
|
||||
results = new ScrollableResultsDecorator(query.scroll(), _validator);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param info
|
||||
* @param session
|
||||
* @return @throws
|
||||
* HibernateException
|
||||
*/
|
||||
private Query getQuery(String hql,ValueListInfo info, Session session)
|
||||
throws HibernateException, ParseException
|
||||
{
|
||||
|
||||
if (hql != null)
|
||||
{
|
||||
return getStatementBuilder().generate(session,
|
||||
new StringBuffer(hql), info.getFilters(),
|
||||
_isRemoveEmptyStrings);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (namedQuery != null)
|
||||
{
|
||||
return session.getNamedQuery(getNamedQuery());
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HibernateException(
|
||||
"Please define any QUERY in value list retrieve adpater!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If focus optimalization is true, it select only focus property. For
|
||||
* validator is recommended to set it to false, while you want to validate
|
||||
* properties of retrieved objects.
|
||||
*
|
||||
* @param info
|
||||
* @param session
|
||||
* @return
|
||||
* @throws HibernateException
|
||||
*/
|
||||
private Query getQueryForFocus(ValueListInfo info, Session session)
|
||||
throws HibernateException, ParseException
|
||||
{
|
||||
if (isFocusOptimalization() == true)
|
||||
{
|
||||
LOGGER.info("Focus will use optimalizated query.");
|
||||
return getOptimizedQuery(info, session);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.info("Focus will use normal (full) query.");
|
||||
// return getQuery(info, session);
|
||||
// ignore focus query altered by wenshuang.ma
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param info
|
||||
* @param session
|
||||
* @return query that select only focus property.
|
||||
* @throws HibernateException
|
||||
* @throws ParseException
|
||||
*/
|
||||
private Query getOptimizedQuery(ValueListInfo info, Session session) throws HibernateException, ParseException
|
||||
{
|
||||
if (getHql() != null)
|
||||
{
|
||||
return getStatementBuilder().generateForFocus(session,
|
||||
new StringBuffer(getHql()), info.getFilters(),
|
||||
_isRemoveEmptyStrings, defaultFocusPropertyObjectAlias,
|
||||
info.getFocusProperty());
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HibernateException(
|
||||
"Please define any HQL QUERY in value list retrieve adpater, function is not implemented for NamedQuery!");
|
||||
}
|
||||
}
|
||||
|
||||
/** Set the Hibernate SessionFactory to be used by this DAO.
|
||||
*
|
||||
* @param sessionFactory The Hibernate SessionFactory to be used by this DAO.
|
||||
*/
|
||||
public final void setSessionFactory(SessionFactory sessionFactory)
|
||||
{
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
/** Return the Hibernate SessionFactory used by this DAO.
|
||||
*
|
||||
* @return The Hibernate SessionFactory used by this DAO.
|
||||
*/
|
||||
protected final SessionFactory getSessionFactory()
|
||||
{
|
||||
return this.sessionFactory;
|
||||
}
|
||||
|
||||
/** Sets the hql used to retrieve the data.
|
||||
* @param query
|
||||
* The hql to set.
|
||||
* @deprecated use setHql(String)
|
||||
*/
|
||||
public void setHsql(String hql)
|
||||
{
|
||||
this.hql = hql;
|
||||
}
|
||||
|
||||
/** Sets the hql used to retrieve the data.
|
||||
* @param query
|
||||
* The hql to set.
|
||||
*/
|
||||
public void setHql(String hql)
|
||||
{
|
||||
this.hql = hql;
|
||||
}
|
||||
|
||||
/** Returns the namedQuery.
|
||||
* @return Returns the namedQuery.
|
||||
*/
|
||||
public String getNamedQuery()
|
||||
{
|
||||
return namedQuery;
|
||||
}
|
||||
|
||||
/** Sets the namedQuery.
|
||||
* <p>
|
||||
* NOTE: by using this you can not enable sorting, filtering,
|
||||
* paging of the data, and focusing of rows.
|
||||
* </p>
|
||||
* @param namedQuery
|
||||
* The namedQuery to set.
|
||||
*/
|
||||
public void setNamedQuery(String namedQuery)
|
||||
{
|
||||
this.namedQuery = namedQuery;
|
||||
}
|
||||
|
||||
/** Gets the hql used to retrieve the data.
|
||||
* @return Returns the hql.
|
||||
*/
|
||||
public String getHql()
|
||||
{
|
||||
return hql;
|
||||
}
|
||||
|
||||
/** Sets: If a new Session should be created if no thread-bound found.
|
||||
*
|
||||
* @param allowCreate
|
||||
* The allowCreate to set.
|
||||
*/
|
||||
public void setAllowCreate(boolean allowCreate)
|
||||
{
|
||||
this.allowCreate = allowCreate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum rows to search with Focus
|
||||
*
|
||||
* @return Returns the maxRowsForFocus.
|
||||
*/
|
||||
public long getMaxRowsForFocus()
|
||||
{
|
||||
return maxRowsForFocus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximum rows to search with Focus
|
||||
*
|
||||
* @param maxRowsForFocus
|
||||
* The maxRowsForFocus to set.
|
||||
*/
|
||||
public void setMaxRowsForFocus(long maxRowsForFocus)
|
||||
{
|
||||
this.maxRowsForFocus = maxRowsForFocus;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the defaultFocusPropertyObject.
|
||||
*/
|
||||
public String getDefaultFocusPropertyObjectAlias()
|
||||
{
|
||||
return defaultFocusPropertyObjectAlias;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of object use to get focus property in hibernate hql syntax
|
||||
* SELECT defaultFocusPropertyObjectAlias.getFocusProperty ...
|
||||
*
|
||||
* @param defaultFocusPropertyObjectAlias
|
||||
* The defaultFocusPropertyObjectAlias to set.
|
||||
*/
|
||||
public void setDefaultFocusPropertyObjectAlias(
|
||||
String defaultFocusPropertyObjectAlias)
|
||||
{
|
||||
this.defaultFocusPropertyObjectAlias = defaultFocusPropertyObjectAlias
|
||||
+ ".";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the statementBuilder.
|
||||
*/
|
||||
public StatementBuilder getStatementBuilder()
|
||||
{
|
||||
if (statementBuilder == null)
|
||||
{
|
||||
statementBuilder = new StatementBuilder();
|
||||
}
|
||||
return statementBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param statementBuilder The statementBuilder to set.
|
||||
*/
|
||||
public void setStatementBuilder(StatementBuilder statementBuilder)
|
||||
{
|
||||
this.statementBuilder = statementBuilder;
|
||||
}
|
||||
public ValueList scrollableQuery(String pureHql, ValueListInfo info, Map queryParams) {
|
||||
if (info.getPagingNumberPer() == Integer.MAX_VALUE)
|
||||
{
|
||||
info.setPagingNumberPer(getDefaultNumberPerPage());
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("The paging number per page '" + getDefaultNumberPerPage() + "' was set.");
|
||||
}
|
||||
}
|
||||
|
||||
Session session = SessionFactoryUtils.getSession(getSessionFactory(),allowCreate);
|
||||
List list;
|
||||
try
|
||||
{
|
||||
Query query = session.createQuery(pureHql);
|
||||
if(cacheRegion != null){
|
||||
query.setCacheable(true);
|
||||
query.setCacheRegion(cacheRegion);
|
||||
query.setCacheMode(CacheMode.NORMAL);
|
||||
}
|
||||
|
||||
for(Object object : queryParams.entrySet()){
|
||||
Map.Entry entry = (Entry) object;
|
||||
if(entry.getValue() instanceof Collection) {
|
||||
query.setParameterList(entry.getKey().toString(),(Collection)entry.getValue());
|
||||
}else{
|
||||
query.setParameter(entry.getKey().toString(),entry.getValue());
|
||||
}
|
||||
}
|
||||
list = new ArrayList(info.getPagingNumberPer());
|
||||
ScrollableResults results = getScrollableResults(query, info);
|
||||
if (results.first())
|
||||
{
|
||||
int pageNumber = info.getPagingPage();
|
||||
int numberPerPage = (info.getPagingNumberPer() > 0) ? info
|
||||
.getPagingNumberPer() : getDefaultNumberPerPage();
|
||||
|
||||
if (info.getPagingPage() > 1)
|
||||
{
|
||||
results.scroll((pageNumber - 1) * numberPerPage);
|
||||
}
|
||||
|
||||
for (int i = 0; i < numberPerPage; i++)
|
||||
{
|
||||
Object[] result = results.get();
|
||||
if(result.length == 1 && result[0] instanceof DomainModel){
|
||||
list.add(result[0]);
|
||||
}
|
||||
else{
|
||||
list.add(result);
|
||||
}
|
||||
if (!results.next())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
results.last();
|
||||
info.setTotalNumberOfEntries(results.getRowNumber() + 1);
|
||||
}
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("Sorting finished.");
|
||||
}
|
||||
ValueList returnValueList = getListBackedValueList(info, list);
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("Retrieved list was wrapped in valuelist, info="
|
||||
+ info);
|
||||
}
|
||||
return returnValueList;
|
||||
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
SessionFactoryUtils.releaseSession(session, getSessionFactory());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public String getCacheRegion() {
|
||||
return cacheRegion;
|
||||
}
|
||||
public void setCacheRegion(String cacheRegion) {
|
||||
this.cacheRegion = cacheRegion;
|
||||
}
|
||||
public ValueList getValueListNoCount(String hql, ValueListInfo info) {
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("getValueList(String, ValueListInfo) - start");
|
||||
}
|
||||
|
||||
if (info.getSortingColumn() == null)
|
||||
{
|
||||
info.setPrimarySortColumn(getDefaultSortColumn());
|
||||
info.setPrimarySortDirection(getDefaultSortDirectionInteger());
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("The default sort column '" + getDefaultSortColumn() + "' with direction '"+getDefaultSortDirectionInteger()+"' was set.");
|
||||
}
|
||||
}
|
||||
|
||||
if (info.getPagingNumberPer() == Integer.MAX_VALUE)
|
||||
{
|
||||
info.setPagingNumberPer(getDefaultNumberPerPage());
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("The paging number per page '" + getDefaultNumberPerPage() + "' was set.");
|
||||
}
|
||||
}
|
||||
Session session = SessionFactoryUtils.getSession(getSessionFactory(),allowCreate);
|
||||
try
|
||||
{
|
||||
Query query = getQuery(hql,info, session);
|
||||
int numberPerPage = info.getPagingNumberPer();
|
||||
ArrayList list = new ArrayList(numberPerPage);
|
||||
ScrollableResults results = getScrollableResults(query, info);
|
||||
results.first();
|
||||
// int lastRowNumber = results.getRowNumber();
|
||||
// info.setTotalNumberOfEntries(lastRowNumber + 1);
|
||||
if (numberPerPage == 0)
|
||||
{
|
||||
numberPerPage = getDefaultNumberPerPage();
|
||||
}
|
||||
|
||||
int pageNumber = info.getPagingPage();
|
||||
boolean isResult;
|
||||
if (pageNumber > 1)
|
||||
{
|
||||
isResult = results.scroll((pageNumber - 1) * numberPerPage);
|
||||
}
|
||||
else
|
||||
{
|
||||
isResult = results.first();
|
||||
}
|
||||
|
||||
for (int i = 0; i < numberPerPage && isResult; i++)
|
||||
{
|
||||
Object[] objs = results.get();
|
||||
if(objs != null && 1 == objs.length){
|
||||
list.add(objs[0]);
|
||||
}
|
||||
else{
|
||||
list.add(objs);
|
||||
}
|
||||
|
||||
isResult = results.next();
|
||||
}
|
||||
results.close();
|
||||
ValueList returnValueList = getListBackedValueList(info, list);
|
||||
if (LOGGER.isDebugEnabled())
|
||||
{
|
||||
LOGGER.debug("Retrieved list was wrapped in valuelist, info="
|
||||
+ info);
|
||||
}
|
||||
return returnValueList;
|
||||
}
|
||||
catch (HibernateException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
throw SessionFactoryUtils.convertHibernateAccessException(e);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
finally{
|
||||
SessionFactoryUtils.releaseSession(session, getSessionFactory());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue