Author: epbernard
Date: 2007-01-21 19:39:26 -0500 (Sun, 21 Jan 2007)
New Revision: 11072
Modified:
branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java
Log:
EJB-263 make elements unique before raising getSingleResult exceptions
Modified: branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java
===================================================================
---
branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java 2007-01-20
19:17:07 UTC (rev 11071)
+++
branches/Branch_3_2/HibernateExt/ejb/src/java/org/hibernate/ejb/QueryImpl.java 2007-01-22
00:39:26 UTC (rev 11072)
@@ -5,6 +5,8 @@
import java.util.Collection;
import java.util.Date;
import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
@@ -82,7 +84,14 @@
em.throwPersistenceException( new NoResultException( "No entity found for
query" ) );
}
else if ( result.size() > 1 ) {
- em.throwPersistenceException( new NonUniqueResultException( "result returns
" + result.size() + " elements") );
+ Set uniqueResult = new HashSet(result);
+ if ( uniqueResult.size() > 1 ) {
+ em.throwPersistenceException( new NonUniqueResultException( "result returns
" + uniqueResult.size() + " elements") );
+ }
+ else {
+ return uniqueResult.iterator().next();
+ }
+
}
else {
return result.get(0);
Show replies by date