Skip to content

Commit 6618387

Browse files
committed
HHH-18979 add equalIgnoringCase()
1 parent d4fcad0 commit 6618387

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

hibernate-core/src/main/java/org/hibernate/query/Restriction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ static <T,U> Restriction<T> unequal(SingularAttribute<T,U> attribute, U value) {
6666
return equal(attribute, value).negated();
6767
}
6868

69+
static <T> Restriction<T> equalIgnoringCase(SingularAttribute<T,String> attribute, String value) {
70+
return restrict( attribute, Range.singleCaseInsensitiveValue(value) );
71+
}
72+
6973
static <T,U> Restriction<T> in(SingularAttribute<T,U> attribute, java.util.List<U> values) {
7074
return restrict( attribute, Range.valueList(values) );
7175
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.query.range;
6+
7+
import jakarta.persistence.criteria.CriteriaBuilder;
8+
import jakarta.persistence.criteria.Expression;
9+
import jakarta.persistence.criteria.Path;
10+
import jakarta.persistence.criteria.Predicate;
11+
import jakarta.persistence.metamodel.SingularAttribute;
12+
13+
import java.util.Locale;
14+
15+
/**
16+
* Restricts to a single literal string, ignoring case.
17+
*/
18+
record CaseInsensitiveValue(String value) implements Range<String> {
19+
@Override
20+
public <X> Predicate toPredicate(Path<? extends X> root, SingularAttribute<X, String> attribute, CriteriaBuilder builder) {
21+
// TODO: it would be much better to not do use literal,
22+
// and let it be treated as a parameter, but we
23+
// we run into the usual bug with parameters in
24+
// manipulated SQM trees
25+
final Expression<String> literal = builder.literal( value.toLowerCase( Locale.ROOT ) );
26+
return builder.lower( root.get( attribute ) ).equalTo( literal );
27+
}
28+
}

hibernate-core/src/main/java/org/hibernate/query/range/Range.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ static <U> Range<U> singleValue(U value) {
3838
return new Value<>( value );
3939
}
4040

41+
static Range<String> singleCaseInsensitiveValue(String value) {
42+
return new CaseInsensitiveValue( value );
43+
}
44+
4145
static <U> Range<U> valueList(java.util.List<U> values) {
4246
return new List<>( values );
4347
}

hibernate-core/src/test/java/org/hibernate/orm/test/query/restriction/RestrictionTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ void test(SessionFactoryScope scope) {
6969
.addRestriction( Restriction.between( pages, 150, 400 ) )
7070
.getResultList() );
7171
assertEquals( 1, booksByPageRange.size() );
72+
Book bookByTitle = scope.fromSession( session ->
73+
session.createSelectionQuery( "from Book", Book.class)
74+
.addRestriction( Restriction.equalIgnoringCase( title, "hibernate in action" ) )
75+
.getSingleResultOrNull() );
76+
assertEquals( "9781932394153", bookByTitle.isbn );
7277
}
7378

7479
@Entity(name="Book")

0 commit comments

Comments
 (0)