diff --git a/src/main/java/com/avaje/ebean/ExpressionList.java b/src/main/java/com/avaje/ebean/ExpressionList.java index 99ea95c76..0a8ba54c2 100644 --- a/src/main/java/com/avaje/ebean/ExpressionList.java +++ b/src/main/java/com/avaje/ebean/ExpressionList.java @@ -266,6 +266,24 @@ public interface ExpressionList extends Serializable { */ Query select(String properties); + /** + * Set whether this query uses DISTINCT. + *

+ * The select() clause MUST be specified when setDistinct(true) is set. The reason for this is that + * generally ORM queries include the "id" property and this doesn't make sense for distinct queries. + *

+ *
{@code
+   *
+   *   List customers =
+   *       Ebean.find(Customer.class)
+   *          .setDistinct(true)
+   *          .select("name")     // only select the customer name
+   *          .findList();
+   *
+   * }
+ */ + Query setDistinct(boolean distinct); + /** * Set the first row to fetch. * diff --git a/src/main/java/com/avaje/ebean/Query.java b/src/main/java/com/avaje/ebean/Query.java index 18076529f..63f27f1df 100644 --- a/src/main/java/com/avaje/ebean/Query.java +++ b/src/main/java/com/avaje/ebean/Query.java @@ -1143,6 +1143,19 @@ public interface Query extends Serializable { /** * Set whether this query uses DISTINCT. + *

+ * The select() clause MUST be specified when setDistinct(true) is set. The reason for this is that + * generally ORM queries include the "id" property and this doesn't make sense for distinct queries. + *

+ *
{@code
+   *
+   *   List customers =
+   *       Ebean.find(Customer.class)
+   *          .setDistinct(true)
+   *          .select("name")
+   *          .findList();
+   *
+   * }
*/ Query setDistinct(boolean isDistinct); diff --git a/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java b/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java index 10ce12a86..2312dc3c1 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java +++ b/src/main/java/com/avaje/ebeaninternal/server/expression/JunctionExpression.java @@ -488,6 +488,11 @@ abstract class JunctionExpression implements Junction, SpiExpression, Expr return exprList.select(properties); } + @Override + public Query setDistinct(boolean distinct) { + return exprList.setDistinct(distinct); + } + @Override public com.avaje.ebean.Query setFirstRow(int firstRow) { return exprList.setFirstRow(firstRow); diff --git a/src/main/java/com/avaje/ebeaninternal/util/DefaultExpressionList.java b/src/main/java/com/avaje/ebeaninternal/util/DefaultExpressionList.java index 8ab8f66d4..eb0147d1c 100644 --- a/src/main/java/com/avaje/ebeaninternal/util/DefaultExpressionList.java +++ b/src/main/java/com/avaje/ebeaninternal/util/DefaultExpressionList.java @@ -238,6 +238,11 @@ public class DefaultExpressionList implements SpiExpressionList { return query.select(fetchProperties); } + @Override + public Query setDistinct(boolean distinct) { + return query.setDistinct(distinct); + } + @Override public Query setFirstRow(int firstRow) { return query.setFirstRow(firstRow); diff --git a/src/test/java/com/avaje/tests/query/other/TestQueryDistinct.java b/src/test/java/com/avaje/tests/query/other/TestQueryDistinct.java index 72cc30fc2..eb85ddfc3 100644 --- a/src/test/java/com/avaje/tests/query/other/TestQueryDistinct.java +++ b/src/test/java/com/avaje/tests/query/other/TestQueryDistinct.java @@ -41,7 +41,22 @@ public class TestQueryDistinct extends BaseTestCase { Assert.assertNull(customer.getAnniversary()); } } - + + @Test + public void test_onWhere() { + + ResetBasicData.reset(); + + Query query = Ebean.find(Customer.class) + .setUseCache(false) + .where().setDistinct(true) + .select("name"); + + query.findList(); + + String generatedSql = query.getGeneratedSql(); + Assert.assertTrue(generatedSql.contains("select distinct t0.name c0 from o_customer t0")); + } @Test public void testDistinctStatus() {