Skip to content

Commit 1346f4a

Browse files
committed
HHH-18979 work on some details
1 parent 48af9bb commit 1346f4a

File tree

14 files changed

+146
-30
lines changed

14 files changed

+146
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public Predicate toPredicate(Root<? extends X> root, CriteriaBuilder builder) {
3434
if ( !(attribute instanceof SingularAttribute) ) {
3535
throw new IllegalArgumentException( "Attribute '" + attributeName + "' is not singular" );
3636
}
37-
if ( !range.getType().isAssignableFrom( attribute.getJavaType() ) ) {
37+
if ( range.getType()!=null && !range.getType().isAssignableFrom( attribute.getJavaType() ) ) {
3838
throw new IllegalArgumentException( "Attribute '" + attributeName
3939
+ "' is not assignable to range of type '" + range.getType().getName() + "'" );
4040
}

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* .setOrder(Order.desc(Book_.title))
2525
* .getResultList() );
2626
* </pre>
27+
* <p>
28+
* Each restriction pairs an {@linkplain SingularAttribute attribute} of the
29+
* entity with a {@link Range} of allowed values for the attribute.
2730
*
2831
* @param <X> The entity result type of the query
2932
*
@@ -53,10 +56,21 @@ public interface Restriction<X> {
5356
@Internal
5457
Predicate toPredicate(Root<? extends X> root, CriteriaBuilder builder);
5558

59+
/**
60+
* Restrict the allowed values of the given attribute to the given
61+
* {@linkplain Range range}.
62+
*/
5663
static <T, U> Restriction<T> restrict(SingularAttribute<T, U> attribute, Range<U> range) {
5764
return new AttributeRange<>( attribute, range );
5865
}
5966

67+
/**
68+
* Restrict the allowed values of the named attribute of the given
69+
* entity class to the given {@linkplain Range range}.
70+
* <p>
71+
* This operation is not compile-time type safe. Prefer the use of
72+
* {@link #restrict(SingularAttribute, Range)}.
73+
*/
6074
static <T> Restriction<T> restrict(Class<T> type, String attributeName, Range<?> range) {
6175
return new NamedAttributeRange<>( type, attributeName, range );
6276
}
@@ -131,26 +145,7 @@ static <T> Restriction<T> or(Restriction<T>... restrictions) {
131145
return new Disjunction<>( java.util.List.of( restrictions ) );
132146
}
133147

134-
static <T> Restriction<T> none() {
135-
return new Restriction<>() {
136-
final Restriction<T> none = this;
137-
@Override
138-
public Restriction<T> negated() {
139-
return new Restriction<>() {
140-
@Override
141-
public Predicate toPredicate(Root<? extends T> root, CriteriaBuilder builder) {
142-
return builder.disjunction();
143-
}
144-
@Override
145-
public Restriction<T> negated() {
146-
return none;
147-
}
148-
};
149-
}
150-
@Override
151-
public Predicate toPredicate(Root<? extends T> root, CriteriaBuilder builder) {
152-
return builder.conjunction();
153-
}
154-
};
148+
static <T> Restriction<T> unrestricted() {
149+
return new Unrestricted<>();
155150
}
156151
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.query;
6+
7+
import jakarta.persistence.criteria.CriteriaBuilder;
8+
import jakarta.persistence.criteria.Predicate;
9+
import jakarta.persistence.criteria.Root;
10+
11+
/**
12+
* A null restriction.
13+
*/
14+
public class Unrestricted<T> implements Restriction<T> {
15+
@Override
16+
public Restriction<T> negated() {
17+
return new Restriction<>() {
18+
@Override
19+
public Predicate toPredicate(Root<? extends T> root, CriteriaBuilder builder) {
20+
return builder.disjunction();
21+
}
22+
23+
@Override
24+
public Restriction<T> negated() {
25+
return Unrestricted.this;
26+
}
27+
};
28+
}
29+
30+
@Override
31+
public Predicate toPredicate(Root<? extends T> root, CriteriaBuilder builder) {
32+
return builder.conjunction();
33+
}
34+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
import jakarta.persistence.criteria.Predicate;
1111

1212
import java.util.Locale;
13+
import java.util.Objects;
1314

1415
/**
15-
* Restricts to a single literal string, ignoring case.
16+
* A {@link Range} with a single literal string, ignoring case.
1617
*/
1718
record CaseInsensitiveValue(String value) implements Range<String> {
19+
CaseInsensitiveValue {
20+
Objects.requireNonNull( value, "value is null" );
21+
}
22+
1823
@Override
1924
public Predicate toPredicate(Path<String> path, CriteriaBuilder builder) {
2025
// TODO: it would be much better to not do use literal,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.Path;
9+
import jakarta.persistence.criteria.Predicate;
10+
11+
/**
12+
* A {@link Range} containing no values.
13+
*/
14+
record EmptyRange<U>(Class<U> type) implements Range<U> {
15+
@Override
16+
public Class<? extends U> getType() {
17+
return type;
18+
}
19+
20+
@Override
21+
public Predicate toPredicate(Path<U> path, CriteriaBuilder builder) {
22+
return builder.disjunction();
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.Path;
9+
import jakarta.persistence.criteria.Predicate;
10+
11+
/**
12+
* A {@link Range} containing every value of the given type.
13+
*/
14+
record FullRange<U>(Class<U> type) implements Range<U> {
15+
@Override
16+
public Class<? extends U> getType() {
17+
return type;
18+
}
19+
20+
@Override
21+
public Predicate toPredicate(Path<U> path, CriteriaBuilder builder) {
22+
return builder.conjunction();
23+
}
24+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import jakarta.persistence.criteria.Predicate;
1010

1111
/**
12-
* Restricts to an upper-bounded and lower-bounded interval.
12+
* An upper-bounded and lower-bounded interval.
1313
*/
1414
record Interval<U extends Comparable<U>>(LowerBound<U> lowerBound, UpperBound<U> upperBound)
1515
implements Range<U> {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
import jakarta.persistence.criteria.Path;
1010
import jakarta.persistence.criteria.Predicate;
1111

12+
import java.util.Objects;
13+
1214
/**
13-
* Restricts to all values higher than a given lower bound.
15+
* The {@link Range} of all values above a given lower bound.
1416
*/
1517
record LowerBound<U extends Comparable<U>>(U bound, boolean open) implements Range<U> {
18+
LowerBound {
19+
Objects.requireNonNull( bound, "bound is null" );
20+
}
21+
1622
@Override
1723
public Predicate toPredicate(Path<U> path, CriteriaBuilder builder) {
1824
// TODO: it would be much better to not do use literal,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Locale;
1212

1313
/**
14-
* Restricts a string by a pattern.
14+
* The {@link Range} of strings recognized by the given pattern.
1515
*/
1616
record Pattern(String pattern, boolean caseSensitive) implements Range<String> {
1717
@Override

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ static <U extends Comparable<U>> Range<U> closed(U lowerBound, U upperBound) {
7878
static Range<String> pattern(String pattern, boolean caseSensitive) {
7979
return new Pattern( pattern, caseSensitive );
8080
}
81+
82+
static <U> Range<U> empty(Class<U> type) {
83+
return new EmptyRange<>( type );
84+
}
85+
86+
static <U> Range<U> full(Class<U> type) {
87+
return new FullRange<>( type );
88+
}
8189
}

0 commit comments

Comments
 (0)