|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.filter; |
| 8 | + |
| 9 | +import jakarta.persistence.Column; |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.GeneratedValue; |
| 12 | +import jakarta.persistence.GenerationType; |
| 13 | +import jakarta.persistence.Id; |
| 14 | +import org.hibernate.SharedSessionContract; |
| 15 | +import org.hibernate.annotations.Filter; |
| 16 | +import org.hibernate.annotations.FilterDef; |
| 17 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 18 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.MethodSource; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.function.BiConsumer; |
| 27 | +import java.util.function.Consumer; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +@DomainModel( |
| 32 | + annotatedClasses = { |
| 33 | + FilterDefinitionOrderTest.AMyEntity.class, |
| 34 | + FilterDefinitionOrderTest.XEntity.class |
| 35 | + } |
| 36 | +) |
| 37 | +@JiraKey("HHH-19036") |
| 38 | +// Real test is if it actually starts, as that's where the filter binding happens, |
| 39 | +// but let's also check that it was actually processed as well: |
| 40 | +public class FilterDefinitionOrderTest extends AbstractStatefulStatelessFilterTest { |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + scope.inTransaction(session -> { |
| 45 | + AMyEntity entity1 = new AMyEntity(); |
| 46 | + entity1.setField("test"); |
| 47 | + AMyEntity entity2 = new AMyEntity(); |
| 48 | + entity2.setField("Hello"); |
| 49 | + session.persist(entity1); |
| 50 | + session.persist(entity2); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + @AfterEach |
| 55 | + void tearDown() { |
| 56 | + scope.inTransaction(session -> { |
| 57 | + session.createMutationQuery("delete from AMyEntity").executeUpdate(); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest |
| 62 | + @MethodSource("transactionKind") |
| 63 | + void smokeTest(BiConsumer<SessionFactoryScope, Consumer<? extends SharedSessionContract>> inTransaction) { |
| 64 | + inTransaction.accept(scope, session -> { |
| 65 | + session.enableFilter("x_filter"); |
| 66 | + List<AMyEntity> entities = session.createQuery("FROM AMyEntity", AMyEntity.class).getResultList(); |
| 67 | + assertThat(entities).hasSize(1) |
| 68 | + .allSatisfy(entity -> assertThat(entity.getField()).isEqualTo("Hello")); |
| 69 | + |
| 70 | + session.disableFilter("x_filter"); |
| 71 | + entities = session.createQuery("FROM AMyEntity", AMyEntity.class).getResultList(); |
| 72 | + assertThat(entities).hasSize(2); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + @Entity(name = "AMyEntity") |
| 77 | + @Filter(name = "x_filter") |
| 78 | + public static class AMyEntity { |
| 79 | + @Id |
| 80 | + @GeneratedValue(strategy = GenerationType.SEQUENCE) |
| 81 | + @Column(nullable = false) |
| 82 | + private Long id; |
| 83 | + |
| 84 | + public String field; |
| 85 | + |
| 86 | + public Long getId() { |
| 87 | + return id; |
| 88 | + } |
| 89 | + |
| 90 | + public void setId(Long id) { |
| 91 | + this.id = id; |
| 92 | + } |
| 93 | + |
| 94 | + public String getField() { |
| 95 | + return field; |
| 96 | + } |
| 97 | + |
| 98 | + public void setField(String field) { |
| 99 | + this.field = field; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Entity(name = "XEntity") |
| 104 | + @FilterDef(name = "x_filter", defaultCondition = "field = 'Hello'") |
| 105 | + public static class XEntity { |
| 106 | + |
| 107 | + @Id |
| 108 | + @GeneratedValue(strategy = GenerationType.SEQUENCE) |
| 109 | + @Column(nullable = false) |
| 110 | + private Long id; |
| 111 | + |
| 112 | + public String field; |
| 113 | + |
| 114 | + public Long getId() { |
| 115 | + return id; |
| 116 | + } |
| 117 | + |
| 118 | + public void setId(Long id) { |
| 119 | + this.id = id; |
| 120 | + } |
| 121 | + |
| 122 | + public String getField() { |
| 123 | + return field; |
| 124 | + } |
| 125 | + |
| 126 | + public void setField(String field) { |
| 127 | + this.field = field; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments