setMaxResults overwritten by NULL after set to 2 by getSingleResult()
---------------------------------------------------------------------
Key: HHH-3931
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-3931
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.3.1
Reporter: dm san
This is where i create a query and call getSingleResult()
// 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 calls query.list() for answer
Class - org.hibernate.ejb.QueryImpl (JAR - hibernate-entitymanager-3.4.0.GA)
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. (is it trying
to say if i have join fetch in my query, it would not use maxRows previously set. If yes,
what should I do to setMaxResult with fetch)
Class - org.hibernate.hql.ast.QueryTranslatorImpl (JAR -hibernate-core-3.3.1.GA.jar)
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
...
}
I have also posted in userforums:
https://forum.hibernate.org/viewtopic.php?f=1&t=997131&p=2412373#...
Thank you..
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira