File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
main/java/org/hibernate/query
test/java/org/hibernate/orm/test/query/restriction Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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" )
You can’t perform that action at this time.
0 commit comments