HHH-7233 - unmuck EntityManager#getSingleResult wrt auto-setting of max results
This commit is contained in:
parent
14d1c626a5
commit
4785d7eb2e
|
@ -277,19 +277,7 @@ public class QueryImpl<X> extends AbstractQueryImpl<X> implements TypedQuery<X>,
|
|||
@SuppressWarnings({ "unchecked", "RedundantCast" })
|
||||
public X getSingleResult() {
|
||||
try {
|
||||
boolean mucked = false;
|
||||
// IMPL NOTE : the mucking with max results here is attempting to help the user from shooting themselves
|
||||
// in the foot in the case where they have a large query by limiting the query results to 2 max
|
||||
// SQLQuery cannot be safely paginated, leaving the user's choice here.
|
||||
if ( getSpecifiedMaxResults() != 1 &&
|
||||
! ( SQLQuery.class.isAssignableFrom( query.getClass() ) ) ) {
|
||||
mucked = true;
|
||||
query.setMaxResults( 2 ); //avoid OOME if the list is huge
|
||||
}
|
||||
List<X> result = query.list();
|
||||
if ( mucked ) {
|
||||
query.setMaxResults( getSpecifiedMaxResults() );
|
||||
}
|
||||
final List<X> result = query.list();
|
||||
|
||||
if ( result.size() == 0 ) {
|
||||
NoResultException nre = new NoResultException( "No entity found for query" );
|
||||
|
@ -297,7 +285,7 @@ public class QueryImpl<X> extends AbstractQueryImpl<X> implements TypedQuery<X>,
|
|||
throw nre;
|
||||
}
|
||||
else if ( result.size() > 1 ) {
|
||||
Set<X> uniqueResult = new HashSet<X>(result);
|
||||
final Set<X> uniqueResult = new HashSet<X>(result);
|
||||
if ( uniqueResult.size() > 1 ) {
|
||||
NonUniqueResultException nure = new NonUniqueResultException( "result returns more than one elements" );
|
||||
getEntityManager().handlePersistenceException( nure );
|
||||
|
@ -306,7 +294,6 @@ public class QueryImpl<X> extends AbstractQueryImpl<X> implements TypedQuery<X>,
|
|||
else {
|
||||
return uniqueResult.iterator().next();
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
return result.get( 0 );
|
||||
|
|
Loading…
Reference in New Issue