diff --git a/src/main/java/io/ebean/Expr.java b/src/main/java/io/ebean/Expr.java
index 3b6ec35b4..62ffee527 100644
--- a/src/main/java/io/ebean/Expr.java
+++ b/src/main/java/io/ebean/Expr.java
@@ -250,6 +250,43 @@ public class Expr {
return Ebean.getExpressionFactory().in(propertyName, values);
}
+ /**
+ * In where null or empty values means that no predicate is added to the query.
+ *
+ * That is, only add the IN predicate if the values are not null or empty.
+ *
+ * Without this we typically need to code an if block to only add
+ * the IN predicate if the collection is not empty like:
+ *
+ */
+ public static Expression inOrEmpty(String propertyName, Collection> values) {
+ return Ebean.getExpressionFactory().inOrEmpty(propertyName, values);
+ }
+
/**
* Id Equal to - ID property is equal to the value.
*/
diff --git a/src/main/java/io/ebean/ExpressionFactory.java b/src/main/java/io/ebean/ExpressionFactory.java
index c37ef6e78..0ea9071e4 100644
--- a/src/main/java/io/ebean/ExpressionFactory.java
+++ b/src/main/java/io/ebean/ExpressionFactory.java
@@ -325,6 +325,41 @@ public interface ExpressionFactory {
*/
Expression in(String propertyName, Collection> values);
+ /**
+ * In where null or empty values means that no predicate is added to the query.
+ *
+ * That is, only add the IN predicate if the values are not null or empty.
+ *
+ * Without this we typically need to code an if block to only add
+ * the IN predicate if the collection is not empty like:
+ *
+ */
+ Expression inOrEmpty(String propertyName, Collection> values);
+
/**
* Not In - property has a value in the array of values.
*/
diff --git a/src/main/java/io/ebean/ExpressionList.java b/src/main/java/io/ebean/ExpressionList.java
index 94d6b3f1d..8f5cc16fb 100644
--- a/src/main/java/io/ebean/ExpressionList.java
+++ b/src/main/java/io/ebean/ExpressionList.java
@@ -1003,6 +1003,41 @@ public interface ExpressionList {
*/
ExpressionList in(String propertyName, Collection> values);
+ /**
+ * In where null or empty values means that no predicate is added to the query.
+ *
+ * That is, only add the IN predicate if the values are not null or empty.
+ *
+ * Without this we typically need to code an if block to only add
+ * the IN predicate if the collection is not empty like:
+ *
+ */
+ ExpressionList inOrEmpty(String propertyName, Collection> values);
+
/**
* In - using a subQuery.
*
diff --git a/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionFactory.java b/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionFactory.java
index 968cc881e..e568c2cbd 100644
--- a/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionFactory.java
+++ b/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionFactory.java
@@ -455,6 +455,16 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
return new InExpression(propertyName, values, false);
}
+ /**
+ * In where null or empty values means that no predicate is added to the query.
+ *
+ * That is, only add the IN predicate if the values are not null or empty.
+ */
+ @Override
+ public Expression inOrEmpty(String propertyName, Collection> values) {
+ return new InExpression(propertyName, values, false, true);
+ }
+
/**
* In - property has a value in the array of values.
*/
diff --git a/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java b/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java
index 1c57f28ab..1372a06d2 100644
--- a/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java
+++ b/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java
@@ -912,6 +912,12 @@ public class DefaultExpressionList implements SpiExpressionList {
return this;
}
+ @Override
+ public ExpressionList inOrEmpty(String propertyName, Collection> values) {
+ add(expr.inOrEmpty(propertyName, values));
+ return this;
+ }
+
@Override
public ExpressionList in(String propertyName, Object... values) {
add(expr.in(propertyName, values));
diff --git a/src/main/java/io/ebeaninternal/server/expression/InExpression.java b/src/main/java/io/ebeaninternal/server/expression/InExpression.java
index ca0ba1730..656b32fe8 100644
--- a/src/main/java/io/ebeaninternal/server/expression/InExpression.java
+++ b/src/main/java/io/ebeaninternal/server/expression/InExpression.java
@@ -17,8 +17,16 @@ import java.util.List;
class InExpression extends AbstractExpression {
+ private static final String SQL_TRUE = "1=1";
+ private static final String SQL_FALSE = "1=0";
+
private final boolean not;
+ /**
+ * Set to true when adding "1=1" predicate (due to null or empty sourceValues).
+ */
+ private final boolean empty;
+
private final Collection> sourceValues;
private List