Skip to content

Commit 53ebe16

Browse files
committed
HHH-18979 add additional static factory methods for Restriction/Range
and add some javadoc
1 parent c91222a commit 53ebe16

File tree

3 files changed

+69
-19
lines changed

3 files changed

+69
-19
lines changed

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
* <p>
3030
* Each restriction pairs an {@linkplain SingularAttribute attribute} of the
3131
* entity with a {@link Range} of allowed values for the attribute.
32+
* <p>
33+
* A parameter of a {@linkplain org.hibernate.annotations.processing.Find
34+
* finder method} or {@linkplain org.hibernate.annotations.processing.HQL
35+
* HQL query method} may be declared with type {@code Restriction<? super E>},
36+
* {@code List<Restriction<? super E>>}, or {@code Restriction<? super E>...}
37+
* (varargs) where {@code E} is the entity type returned by the query.
3238
*
3339
* @param <X> The entity result type of the query
3440
*
@@ -163,19 +169,35 @@ static <T> Restriction<T> notLike(SingularAttribute<T, String> attribute, String
163169
}
164170

165171
static <T> Restriction<T> startsWith(SingularAttribute<T, String> attribute, String prefix) {
166-
return like( attribute, escape( prefix ) + '%' );
172+
return startsWith( attribute, prefix, true );
167173
}
168174

169-
static <T> Restriction<T> endWith(SingularAttribute<T, String> attribute, String suffix) {
170-
return like( attribute, '%' + escape( suffix ) );
175+
static <T> Restriction<T> endsWith(SingularAttribute<T, String> attribute, String suffix) {
176+
return endsWith( attribute, suffix, true );
171177
}
172178

173179
static <T> Restriction<T> contains(SingularAttribute<T, String> attribute, String substring) {
174-
return like( attribute, '%' + escape( substring ) + '%' );
180+
return contains( attribute, substring, true );
175181
}
176182

177183
static <T> Restriction<T> notContains(SingularAttribute<T, String> attribute, String substring) {
178-
return contains( attribute, substring ).negated();
184+
return notContains( attribute, substring, true );
185+
}
186+
187+
static <T> Restriction<T> startsWith(SingularAttribute<T, String> attribute, String prefix, boolean caseSensitive) {
188+
return restrict( attribute, Range.prefix( prefix, caseSensitive ) );
189+
}
190+
191+
static <T> Restriction<T> endsWith(SingularAttribute<T, String> attribute, String suffix, boolean caseSensitive) {
192+
return restrict( attribute, Range.suffix( suffix, caseSensitive ) );
193+
}
194+
195+
static <T> Restriction<T> contains(SingularAttribute<T, String> attribute, String substring, boolean caseSensitive) {
196+
return restrict( attribute, Range.containing( substring, caseSensitive ) );
197+
}
198+
199+
static <T> Restriction<T> notContains(SingularAttribute<T, String> attribute, String substring, boolean caseSensitive) {
200+
return contains( attribute, substring, caseSensitive ).negated();
179201
}
180202

181203
static <T> Restriction<T> all(List<? extends Restriction<? super T>> restrictions) {
@@ -199,16 +221,4 @@ static <T> Restriction<T> any(Restriction<? super T>... restrictions) {
199221
static <T> Restriction<T> unrestricted() {
200222
return new Unrestricted<>();
201223
}
202-
203-
private static String escape(String literal) {
204-
final var result = new StringBuilder();
205-
for ( int i = 0; i < literal.length(); i++ ) {
206-
final char ch = literal.charAt( i );
207-
if ( ch=='%' || ch=='_' || ch=='\\' ) {
208-
result.append('\\');
209-
}
210-
result.append( ch );
211-
}
212-
return result.toString();
213-
}
214224
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
/**
1717
* Specifies an allowed set of range of values for a value being restricted.
18+
* <p>
19+
* A parameter of a {@linkplain org.hibernate.annotations.processing.Find
20+
* finder method} may be declared with type {@code Range<T>} where {@code T}
21+
* is the type of the matching field of property of the entity.
1822
*
1923
* @param <U> The type of the value being restricted
2024
*
@@ -87,11 +91,47 @@ static Range<String> pattern(String pattern) {
8791
return pattern( pattern, true );
8892
}
8993

94+
static Range<String> prefix(String prefix, boolean caseSensitive) {
95+
return pattern( escape( prefix ) + '%', caseSensitive );
96+
}
97+
98+
static Range<String> suffix(String suffix, boolean caseSensitive) {
99+
return pattern( '%' + escape( suffix ), caseSensitive );
100+
}
101+
102+
static Range<String> containing(String substring, boolean caseSensitive) {
103+
return pattern( '%' + escape( substring ) + '%', caseSensitive );
104+
}
105+
106+
static Range<String> prefix(String prefix) {
107+
return prefix( prefix, true );
108+
}
109+
110+
static Range<String> suffix(String suffix) {
111+
return pattern( suffix, true );
112+
}
113+
114+
static Range<String> containing(String substring) {
115+
return pattern( substring, true );
116+
}
117+
90118
static <U> Range<U> empty(Class<U> type) {
91119
return new EmptyRange<>( type );
92120
}
93121

94122
static <U> Range<U> full(Class<U> type) {
95123
return new FullRange<>( type );
96124
}
125+
126+
private static String escape(String literal) {
127+
final var result = new StringBuilder();
128+
for ( int i = 0; i < literal.length(); i++ ) {
129+
final char ch = literal.charAt( i );
130+
if ( ch=='%' || ch=='_' || ch=='\\' ) {
131+
result.append('\\');
132+
}
133+
result.append( ch );
134+
}
135+
return result.toString();
136+
}
97137
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import static org.hibernate.query.Restriction.any;
2121
import static org.hibernate.query.Restriction.between;
2222
import static org.hibernate.query.Restriction.contains;
23-
import static org.hibernate.query.Restriction.endWith;
23+
import static org.hibernate.query.Restriction.endsWith;
2424
import static org.hibernate.query.Restriction.equal;
2525
import static org.hibernate.query.Restriction.equalIgnoringCase;
2626
import static org.hibernate.query.Restriction.greaterThan;
@@ -105,7 +105,7 @@ void test(SessionFactoryScope scope) {
105105
assertEquals( 0, noBooks.size() );
106106
List<Book> books1 = scope.fromSession( session ->
107107
session.createSelectionQuery( "from Book", Book.class)
108-
.addRestriction( endWith(title, "Hibernate") )
108+
.addRestriction( endsWith(title, "Hibernate") )
109109
.getResultList() );
110110
assertEquals( 1, books1.size() );
111111
List<Book> books2 = scope.fromSession( session ->

0 commit comments

Comments
 (0)