Skip to content

Commit 78e03f5

Browse files
committed
HHH-18979 add Restriction.none() as useful degenerate case
1 parent b99938d commit 78e03f5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,27 @@ static <T> Restriction<T> and(Restriction<T>... restrictions) {
130130
static <T> Restriction<T> or(Restriction<T>... restrictions) {
131131
return new Disjunction<>( java.util.List.of( restrictions ) );
132132
}
133+
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+
};
155+
}
133156
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ void test(SessionFactoryScope scope) {
8181
Range.singleCaseInsensitiveValue("hibernate in action") ) )
8282
.getSingleResultOrNull() );
8383
assertEquals( "9781932394153", bookByTitleUnsafe.isbn );
84+
List<Book> allBooks = scope.fromSession( session ->
85+
session.createSelectionQuery( "from Book", Book.class)
86+
.addRestriction( Restriction.none() )
87+
.getResultList() );
88+
assertEquals( 2, allBooks.size() );
89+
List<Book> noBooks = scope.fromSession( session ->
90+
session.createSelectionQuery( "from Book", Book.class)
91+
.addRestriction( Restriction.none().negated() )
92+
.getResultList() );
93+
assertEquals( 0, noBooks.size() );
8494
}
8595

8696
@Entity(name="Book")

0 commit comments

Comments
 (0)