|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.jpa.criteria.basic; |
| 6 | + |
| 7 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 8 | +import jakarta.persistence.criteria.CriteriaQuery; |
| 9 | +import jakarta.persistence.criteria.Root; |
| 10 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 11 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 14 | +import org.junit.jupiter.api.AfterEach; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * Add test which composes not with in. Test introduced after discovery the negated in |
| 25 | + * fails under dialects without record-level construction, such as DB2. |
| 26 | + * |
| 27 | + * @author Mike Mannion |
| 28 | + */ |
| 29 | +@JiraKey(value = "HHH-19497") |
| 30 | +@DomainModel( |
| 31 | + annotatedClasses = {Foo.class, FooType.class} |
| 32 | +) |
| 33 | +@SessionFactory |
| 34 | +class NegatedInPredicateTest { |
| 35 | + |
| 36 | + public static final FooType FOO_TYPE1 = new FooType( "ft1", "ctx1" ); |
| 37 | + public static final FooType FOO_TYPE2 = new FooType( "ft2", "ctx1" ); |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setup(SessionFactoryScope scope) { |
| 41 | + scope.inTransaction( |
| 42 | + em -> { |
| 43 | + em.persist( FOO_TYPE1 ); |
| 44 | + em.persist( FOO_TYPE2 ); |
| 45 | + |
| 46 | + Foo foo1 = new Foo( 1L, FOO_TYPE1 ); |
| 47 | + Foo foo2 = new Foo( 2L, FOO_TYPE1 ); |
| 48 | + Foo foo3 = new Foo( 3L, FOO_TYPE2 ); |
| 49 | + |
| 50 | + em.persist( foo1 ); |
| 51 | + em.persist( foo2 ); |
| 52 | + em.persist( foo3 ); |
| 53 | + } |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterEach |
| 58 | + void tearDown(SessionFactoryScope scope) { |
| 59 | + scope.inTransaction( |
| 60 | + em -> { |
| 61 | + em.createQuery( "delete from Foo" ).executeUpdate(); |
| 62 | + em.createQuery( "delete from FooType" ).executeUpdate(); |
| 63 | + } |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testSanity(SessionFactoryScope scope) { |
| 69 | + scope.inTransaction( |
| 70 | + em -> { |
| 71 | + CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 72 | + CriteriaQuery<Foo> cq = cb.createQuery( Foo.class ); |
| 73 | + Root<Foo> root = cq.from( Foo.class ); |
| 74 | + assertThat( em.createQuery( cq.select( root ) ).getResultList() ) |
| 75 | + .hasSize( 3 ); |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testNegatedPredicate(SessionFactoryScope scope) { |
| 82 | + scope.inTransaction( |
| 83 | + em -> { |
| 84 | + CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 85 | + CriteriaQuery<Foo> cq = cb.createQuery( Foo.class ); |
| 86 | + Root<Foo> root = cq.from( Foo.class ); |
| 87 | + cq.select( root ) |
| 88 | + .where( cb.not( root.get( "fooType" ).in( List.of( FOO_TYPE1, FOO_TYPE2 ) ) ) ); |
| 89 | + assertThat( em.createQuery( cq ).getResultList() ) |
| 90 | + .isEmpty(); |
| 91 | + } |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testNonNegatedInPredicate(SessionFactoryScope scope) { |
| 97 | + scope.inTransaction( |
| 98 | + em -> { |
| 99 | + CriteriaBuilder cb = em.getCriteriaBuilder(); |
| 100 | + CriteriaQuery<Foo> cq = cb.createQuery( Foo.class ); |
| 101 | + Root<Foo> root = cq.from( Foo.class ); |
| 102 | + cq.select( root ) |
| 103 | + .where( root.get( "fooType" ).in( List.of( FOO_TYPE1, FOO_TYPE2 ) ) ); |
| 104 | + assertThat( em.createQuery( cq ).getResultList() ) |
| 105 | + .hasSize( 3 ); |
| 106 | + |
| 107 | + } |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments