From c3eae0d3a545f28ec3ff1205b7372a8d9d6b19e1 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Fri, 4 Dec 2015 11:12:45 +1300 Subject: [PATCH] #477 - findUnique() should throw NonUniqueResultException and not PersistenceException --- .../java/com/avaje/ebean/EbeanServer.java | 11 ++++++--- .../java/com/avaje/ebean/ExpressionList.java | 12 ++++++++-- src/main/java/com/avaje/ebean/Query.java | 5 +++- .../server/core/DefaultServer.java | 24 +++++++------------ 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/avaje/ebean/EbeanServer.java b/src/main/java/com/avaje/ebean/EbeanServer.java index 459ab6b4d..0bf18b041 100644 --- a/src/main/java/com/avaje/ebean/EbeanServer.java +++ b/src/main/java/com/avaje/ebean/EbeanServer.java @@ -9,6 +9,7 @@ import com.avaje.ebean.text.csv.CsvReader; import com.avaje.ebean.text.json.JsonContext; import org.jetbrains.annotations.Nullable; +import javax.persistence.NonUniqueResultException; import javax.persistence.OptimisticLockException; import javax.persistence.PersistenceException; import java.util.Collection; @@ -1091,14 +1092,17 @@ public interface EbeanServer { Map findMap(Query query, Transaction transaction); /** - * Execute the query returning at most one entity bean. This will throw a - * PersistenceException if the query finds more than one result. + * Execute the query returning at most one entity bean or null (if no matching + * bean is found). + *

+ * This will throw a NonUniqueResultException if the query finds more than one result. + *

*

* Generally you are able to use {@link Query#findUnique()} rather than * explicitly calling this method. You could use this method if you wish to * explicitly control the transaction used for the query. *

- * + * * @param * the type of entity bean to fetch. * @param query @@ -1106,6 +1110,7 @@ public interface EbeanServer { * @param transaction * the transaction to use (can be null). * @return the list of fetched beans. + * @throws NonUniqueResultException if more than one result was found * * @see Query#findUnique() */ diff --git a/src/main/java/com/avaje/ebean/ExpressionList.java b/src/main/java/com/avaje/ebean/ExpressionList.java index e619a4fe4..b91237c20 100644 --- a/src/main/java/com/avaje/ebean/ExpressionList.java +++ b/src/main/java/com/avaje/ebean/ExpressionList.java @@ -3,6 +3,7 @@ package com.avaje.ebean; import com.avaje.ebean.text.PathProperties; import org.jetbrains.annotations.Nullable; +import javax.persistence.NonUniqueResultException; import java.io.Serializable; import java.sql.Timestamp; import java.util.Collection; @@ -193,8 +194,15 @@ public interface ExpressionList extends Serializable { Map findMap(String keyProperty, Class keyType); /** - * Execute the query returning a single bean. - * + * Execute the query returning a single bean or null (if no matching + * bean is found). + *

+ * If more than 1 row is found for this query then a NonUniqueResultException is + * thrown. + *

+ * + * @throws NonUniqueResultException if more than one result was found + * * @see Query#findUnique() */ @Nullable diff --git a/src/main/java/com/avaje/ebean/Query.java b/src/main/java/com/avaje/ebean/Query.java index 7043fd0f7..d1978b07a 100644 --- a/src/main/java/com/avaje/ebean/Query.java +++ b/src/main/java/com/avaje/ebean/Query.java @@ -3,6 +3,7 @@ package com.avaje.ebean; import com.avaje.ebean.text.PathProperties; import org.jetbrains.annotations.Nullable; +import javax.persistence.NonUniqueResultException; import java.io.Serializable; import java.sql.Timestamp; import java.util.List; @@ -700,7 +701,7 @@ public interface Query extends Serializable { * Execute the query returning either a single bean or null (if no matching * bean is found). *

- * If more than 1 row is found for this query then a PersistenceException is + * If more than 1 row is found for this query then a NonUniqueResultException is * thrown. *

*

@@ -736,6 +737,8 @@ public interface Query extends Serializable { * List details = order.getDetails(); * ... * } + * + * @throws NonUniqueResultException if more than one result was found */ @Nullable T findUnique(); diff --git a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java index 3b0421ec6..429665cc2 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java +++ b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java @@ -70,6 +70,7 @@ import com.avaje.ebeaninternal.util.ParamTypeHelper.TypeInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.persistence.NonUniqueResultException; import javax.persistence.OptimisticLockException; import javax.persistence.PersistenceException; import java.util.ArrayList; @@ -1218,13 +1219,16 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { // a query that is expected to return either 0 or 1 rows List list = findList(query, t); + return extractUnique(list); + } - if (list.size() == 0) { + private T extractUnique(List list) { + if (list.isEmpty()) { return null; - + } else if (list.size() > 1) { - throw new PersistenceException("Unique expecting 0 or 1 rows but got [" + list.size() + "]"); - + throw new NonUniqueResultException("Unique expecting 0 or 1 results but got [" + list.size() + "]"); + } else { return list.get(0); } @@ -1449,17 +1453,7 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer { // no findId() method for SqlQuery... // a query that is expected to return either 0 or 1 rows List list = findList(query, t); - - if (list.size() == 0) { - return null; - - } else if (list.size() > 1) { - String m = "Unique expecting 0 or 1 rows but got [" + list.size() + "]"; - throw new PersistenceException(m); - - } else { - return list.get(0); - } + return extractUnique(list); } public SqlFutureList findFutureList(SqlQuery query, Transaction t) {