Problem - maxRows get overwriiten by null(actually they are not copied over) in QueryTranslatorImpl
I have tried in every different way. Read many many articles, But could not get an answer. Please Help !!!
This is where i
create a query and call getSingleResult()Code:
// This is in my project
Query query = em.createQuery(SELECT_CLAUSE + "where ed.id=?1"); // select clause has left join fetch
//query.setFirstResult(0).setMaxResults(2);
query.setParameter(1, id);
ABCD ede = (ABCD) query.getSingleResult(); // ABCD is some Entity
Method getSingleResult which sets the Max result to 2
(code -query.setMaxResults( 2 ) ). and
then call query.list() for answer
Class - org.hibernate.ejb.QueryImpl (JAR - hibernate-entitymanager-3.4.0.GA)Code:
public Object getSingleResult() {
try {
List result;
/* Avoid OOME if the list() is huge (user faulty query) by limiting the query to 2 elements max */
//FIXME: get rid of this impl binding (HHH-3432)
if ( query instanceof AbstractQueryImpl ) {
if (maxResults != 1) query.setMaxResults( 2 ); //avoid OOME if the list is huge
result = query.list();
if ( maxResults != -1 ) {
query.setMaxResults( maxResults ); //put back the original value
}
else {
AbstractQueryImpl queryImpl = AbstractQueryImpl.class.cast( query );
queryImpl.getSelection().setMaxRows( null );
}
...
...// Code not required
...
}
So when getSingleResult is called, it sets the max rows to 2. Now the problem is maxResult always gets overwritten by null.
queryParametersToUse = queryParameters.createCopyUsing( selection ); - this new selection object copies over Fetchsize and Timeout but not maxResults. Hence the new queryParameters does not have value 2.
There is a log warning (
log.warn( "firstResult/maxResults specified with collection fetch; applying in memory!" );) about it but i could not understand it.
Class - org.hibernate.hql.ast.QueryTranslatorImpl (JAR -hibernate-core-3.3.1.GA.jar)Code:
public List list(SessionImplementor session, QueryParameters queryParameters)
throws HibernateException {
log.warn("Call in QuertTranslatorImpl Method Entry");
// Delegate to the QueryLoader...
errorIfDML();
QueryNode query = ( QueryNode ) sqlAst;
boolean hasLimit = queryParameters.getRowSelection() != null && queryParameters.getRowSelection().definesLimits();
boolean needsDistincting = ( query.getSelectClause().isDistinct() || hasLimit ) && containsCollectionFetches();
QueryParameters queryParametersToUse;
if ( hasLimit && containsCollectionFetches() ) {
log.warn( "firstResult/maxResults specified with collection fetch; applying in memory!" );
RowSelection selection = new RowSelection();
selection.setFetchSize( queryParameters.getRowSelection().getFetchSize() );
selection.setTimeout( queryParameters.getRowSelection().getTimeout() );
//selection.setMaxRows(queryParameters.getRowSelection().getMaxRows()); // If i include this line,everything works fine as Maxresults is also copied over
queryParametersToUse = queryParameters.createCopyUsing( selection );
}
else {
queryParametersToUse = queryParameters;
}
List results = queryLoader.list( session, queryParametersToUse );
...
...// Code not required
...
}
Thank you..