Skip to content

Commit 4785d7e

Browse files
committed
HHH-7233 - unmuck EntityManager#getSingleResult wrt auto-setting of max results
1 parent 14d1c62 commit 4785d7e

File tree

1 file changed

+2
-15
lines changed
  • hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal

1 file changed

+2
-15
lines changed

hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/QueryImpl.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -277,27 +277,15 @@ public List<X> getResultList() {
277277
@SuppressWarnings({ "unchecked", "RedundantCast" })
278278
public X getSingleResult() {
279279
try {
280-
boolean mucked = false;
281-
// IMPL NOTE : the mucking with max results here is attempting to help the user from shooting themselves
282-
// in the foot in the case where they have a large query by limiting the query results to 2 max
283-
// SQLQuery cannot be safely paginated, leaving the user's choice here.
284-
if ( getSpecifiedMaxResults() != 1 &&
285-
! ( SQLQuery.class.isAssignableFrom( query.getClass() ) ) ) {
286-
mucked = true;
287-
query.setMaxResults( 2 ); //avoid OOME if the list is huge
288-
}
289-
List<X> result = query.list();
290-
if ( mucked ) {
291-
query.setMaxResults( getSpecifiedMaxResults() );
292-
}
280+
final List<X> result = query.list();
293281

294282
if ( result.size() == 0 ) {
295283
NoResultException nre = new NoResultException( "No entity found for query" );
296284
getEntityManager().handlePersistenceException( nre );
297285
throw nre;
298286
}
299287
else if ( result.size() > 1 ) {
300-
Set<X> uniqueResult = new HashSet<X>(result);
288+
final Set<X> uniqueResult = new HashSet<X>(result);
301289
if ( uniqueResult.size() > 1 ) {
302290
NonUniqueResultException nure = new NonUniqueResultException( "result returns more than one elements" );
303291
getEntityManager().handlePersistenceException( nure );
@@ -306,7 +294,6 @@ else if ( result.size() > 1 ) {
306294
else {
307295
return uniqueResult.iterator().next();
308296
}
309-
310297
}
311298
else {
312299
return result.get( 0 );

0 commit comments

Comments
 (0)