From 53d474d85fd70cd359a1712546b5fea5e6e9a504 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 30 May 2023 11:20:09 +0200 Subject: [PATCH] HHH-16661 Add test for issue --- .../SingleTableDefaultSchemaFilterTest.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/filter/subclass/singletable/SingleTableDefaultSchemaFilterTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/filter/subclass/singletable/SingleTableDefaultSchemaFilterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/filter/subclass/singletable/SingleTableDefaultSchemaFilterTest.java new file mode 100644 index 0000000000..f23b76478b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/filter/subclass/singletable/SingleTableDefaultSchemaFilterTest.java @@ -0,0 +1,124 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.filter.subclass.singletable; + +import java.util.List; + +import org.hibernate.annotations.Filter; +import org.hibernate.annotations.FilterDef; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.query.MutationQuery; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.DiscriminatorColumn; +import jakarta.persistence.DiscriminatorType; +import jakarta.persistence.DiscriminatorValue; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Marco Belladelli + */ +@SessionFactory +@DomainModel( + annotatedClasses = { + SingleTableDefaultSchemaFilterTest.AbstractSuperClass.class, + SingleTableDefaultSchemaFilterTest.ChildEntityOne.class, + SingleTableDefaultSchemaFilterTest.ChildEntityTwo.class + } +) +@ServiceRegistry( settings = @Setting( name = AvailableSettings.DEFAULT_SCHEMA, value = "public" ) ) +@RequiresDialect( H2Dialect.class ) +@Jira( "https://hibernate.atlassian.net/browse/HHH-16661" ) +public class SingleTableDefaultSchemaFilterTest { + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.persist( new ChildEntityOne() ); + session.persist( new ChildEntityTwo() ); + } ); + } + + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( + session -> session.createMutationQuery( "delete from AbstractSuperClass" ).executeUpdate() + ); + } + + @Test + public void testUpdate(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.enableFilter( "dummy_filter" ); + final MutationQuery updateQuery = session.createMutationQuery( + "update ChildEntityTwo cet set cet.name = 'John'" ); + final int updated = updateQuery.executeUpdate(); + assertEquals( 1, updated ); + } ); + scope.inTransaction( session -> { + session.enableFilter( "dummy_filter" ); + final List resultList = session.createQuery( + "select p from AbstractSuperClass p where p.name = 'John'", + AbstractSuperClass.class + ).getResultList(); + assertEquals( 1, resultList.size() ); + } ); + } + + @Test + public void testDelete(SessionFactoryScope scope) { + scope.inTransaction( session -> { + session.enableFilter( "dummy_filter" ); + final MutationQuery deleteQuery = session.createMutationQuery( "delete from ChildEntityOne" ); + final int deleted = deleteQuery.executeUpdate(); + assertEquals( 1, deleted ); + } ); + scope.inTransaction( session -> { + session.enableFilter( "dummy_filter" ); + final List resultList = session.createQuery( + "select p from AbstractSuperClass p", + AbstractSuperClass.class + ).getResultList(); + assertEquals( 1, resultList.size() ); + } ); + } + + @Entity( name = "AbstractSuperClass" ) + @DiscriminatorColumn( name = "disc_col", discriminatorType = DiscriminatorType.INTEGER ) + @FilterDef( name = "dummy_filter", defaultCondition = "(id is not null)" ) + @Filter( name = "dummy_filter" ) + public static abstract class AbstractSuperClass { + @Id + @GeneratedValue + Integer id; + String name; + } + + @Entity( name = "ChildEntityOne" ) + @DiscriminatorValue( "1" ) + public static class ChildEntityOne extends AbstractSuperClass { + } + + @Entity( name = "ChildEntityTwo" ) + @DiscriminatorValue( "2" ) + public static class ChildEntityTwo extends AbstractSuperClass { + } +}