From 04c38f189b855535d7fdb35d985a299130456af0 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Thu, 28 Feb 2013 16:43:27 -0500 Subject: [PATCH] HHH-8025 Implemented eqOrIsNull and neOrIsNotNull criteria --- .../org/hibernate/criterion/Property.java | 8 +++++ .../org/hibernate/criterion/Restrictions.java | 30 +++++++++++++++++-- .../test/criteria/CriteriaQueryTest.java | 2 +- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/criterion/Property.java b/hibernate-core/src/main/java/org/hibernate/criterion/Property.java index d988d6cc03..ef50ae62d8 100755 --- a/hibernate-core/src/main/java/org/hibernate/criterion/Property.java +++ b/hibernate-core/src/main/java/org/hibernate/criterion/Property.java @@ -59,10 +59,18 @@ public class Property extends PropertyProjection { return Restrictions.eq(getPropertyName(), value); } + public Criterion eqOrIsNull(Object value) { + return Restrictions.eqOrIsNull(getPropertyName(), value); + } + public SimpleExpression ne(Object value) { return Restrictions.ne(getPropertyName(), value); } + public Criterion neOrIsNotNull(Object value) { + return Restrictions.neOrIsNotNull(getPropertyName(), value); + } + public SimpleExpression gt(Object value) { return Restrictions.gt(getPropertyName(), value); } diff --git a/hibernate-core/src/main/java/org/hibernate/criterion/Restrictions.java b/hibernate-core/src/main/java/org/hibernate/criterion/Restrictions.java index 6c0ee654f5..b9fdbfa386 100755 --- a/hibernate-core/src/main/java/org/hibernate/criterion/Restrictions.java +++ b/hibernate-core/src/main/java/org/hibernate/criterion/Restrictions.java @@ -59,20 +59,46 @@ public class Restrictions { * Apply an "equal" constraint to the named property * @param propertyName * @param value - * @return Criterion + * @return SimpleExpression */ public static SimpleExpression eq(String propertyName, Object value) { return new SimpleExpression(propertyName, value, "="); } + /** + * Apply an "equal" constraint to the named property. If the value + * is null, instead apply "is null". + * @param propertyName + * @param value + * @return Criterion + */ + public static Criterion eqOrIsNull(String propertyName, Object value) { + if (value == null) { + return isNull(propertyName); + } + return new SimpleExpression(propertyName, value, "="); + } /** * Apply a "not equal" constraint to the named property * @param propertyName * @param value - * @return Criterion + * @return SimpleExpression */ public static SimpleExpression ne(String propertyName, Object value) { return new SimpleExpression(propertyName, value, "<>"); } + /** + * Apply a "not equal" constraint to the named property. If the value + * is null, instead apply "is not null". + * @param propertyName + * @param value + * @return Criterion + */ + public static Criterion neOrIsNotNull(String propertyName, Object value) { + if (value == null) { + return isNotNull(propertyName); + } + return new SimpleExpression(propertyName, value, "<>"); + } /** * Apply a "like" constraint to the named property * @param propertyName diff --git a/hibernate-core/src/test/java/org/hibernate/test/criteria/CriteriaQueryTest.java b/hibernate-core/src/test/java/org/hibernate/test/criteria/CriteriaQueryTest.java index 6f30e99804..2fcfdb1d36 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/criteria/CriteriaQueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/criteria/CriteriaQueryTest.java @@ -2064,7 +2064,7 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase { // Ensure Restrictions creates "where foo is null", instead of // "where foo = null" List courses = s.createCriteria( Course.class ).add( - Restrictions.eq( "description", null) ).list(); + Restrictions.eqOrIsNull( "description", null) ).list(); assertEquals( courses.size(), 1 ); assertEquals( courses.get( 0 ).getCourseCode(), course.getCourseCode() );