|
| 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.inheritance; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import jakarta.persistence.DiscriminatorColumn; |
| 21 | +import jakarta.persistence.DiscriminatorType; |
| 22 | +import jakarta.persistence.DiscriminatorValue; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.GeneratedValue; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.JoinColumn; |
| 27 | +import jakarta.persistence.OneToMany; |
| 28 | +import jakarta.persistence.Table; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Marco Belladelli |
| 34 | + */ |
| 35 | +@DomainModel( annotatedClasses = { |
| 36 | + InheritanceJunctionExistsPredicateTest.AbstractEntity.class, |
| 37 | + InheritanceJunctionExistsPredicateTest.EntityA.class, |
| 38 | + InheritanceJunctionExistsPredicateTest.EntityB.class, |
| 39 | + InheritanceJunctionExistsPredicateTest.EntityAContainer.class, |
| 40 | + InheritanceJunctionExistsPredicateTest.EntityBContainer.class, |
| 41 | +} ) |
| 42 | +@SessionFactory( useCollectingStatementInspector = true ) |
| 43 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18174" ) |
| 44 | +public class InheritanceJunctionExistsPredicateTest { |
| 45 | + @Test |
| 46 | + public void testExistsDisjunction(SessionFactoryScope scope) { |
| 47 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 48 | + "select c from EntityAContainer c " |
| 49 | + + "where exists (select 1 from c.entities e where e.identifier like 'child%') " |
| 50 | + + "or exists (select 1 from c.entities e)", |
| 51 | + EntityAContainer.class |
| 52 | + ).getResultList() ).hasSize( 0 ) ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testExistsConjunction(SessionFactoryScope scope) { |
| 57 | + scope.inTransaction( session -> assertThat( session.createQuery( |
| 58 | + "select c from EntityBContainer c " |
| 59 | + + "where exists (select 1 from c.entities e where e.identifier like 'child%') " |
| 60 | + + "and exists (select 1 from c.entities e)", |
| 61 | + EntityBContainer.class |
| 62 | + ).getResultList() ).hasSize( 1 ) ); |
| 63 | + } |
| 64 | + |
| 65 | + @BeforeAll |
| 66 | + public void setUp(SessionFactoryScope scope) { |
| 67 | + scope.inTransaction( session -> { |
| 68 | + final EntityB entityB = new EntityB(); |
| 69 | + entityB.setIdentifier( "child_b" ); |
| 70 | + session.persist( entityB ); |
| 71 | + final EntityAContainer containerA = new EntityAContainer(); |
| 72 | + containerA.id = 1; |
| 73 | + session.persist( containerA ); |
| 74 | + final EntityBContainer containerB = new EntityBContainer(); |
| 75 | + containerB.id = 1; |
| 76 | + containerB.entities.add( entityB ); |
| 77 | + session.persist( containerB ); |
| 78 | + } ); |
| 79 | + } |
| 80 | + |
| 81 | + @AfterAll |
| 82 | + public void tearDown(SessionFactoryScope scope) { |
| 83 | + scope.inTransaction( session -> { |
| 84 | + session.createMutationQuery( "delete from AbstractEntity" ).executeUpdate(); |
| 85 | + session.createMutationQuery( "delete from EntityAContainer" ).executeUpdate(); |
| 86 | + session.createMutationQuery( "delete from EntityBContainer" ).executeUpdate(); |
| 87 | + } ); |
| 88 | + } |
| 89 | + |
| 90 | + @Entity( name = "AbstractEntity" ) |
| 91 | + @DiscriminatorColumn( name = "disc_col", discriminatorType = DiscriminatorType.INTEGER ) |
| 92 | + static abstract class AbstractEntity { |
| 93 | + @Id |
| 94 | + @GeneratedValue |
| 95 | + private Integer id; |
| 96 | + |
| 97 | + private String identifier; |
| 98 | + |
| 99 | + public String getIdentifier() { |
| 100 | + return identifier; |
| 101 | + } |
| 102 | + |
| 103 | + public void setIdentifier(String identifier) { |
| 104 | + this.identifier = identifier; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Entity( name = "EntityA" ) |
| 109 | + @DiscriminatorValue( "1" ) |
| 110 | + static class EntityA extends AbstractEntity { |
| 111 | + } |
| 112 | + |
| 113 | + @Entity( name = "EntityB" ) |
| 114 | + @DiscriminatorValue( "2" ) |
| 115 | + static class EntityB extends AbstractEntity { |
| 116 | + } |
| 117 | + |
| 118 | + @Entity( name = "EntityAContainer" ) |
| 119 | + @Table( name = "a_container" ) |
| 120 | + static class EntityAContainer { |
| 121 | + @Id |
| 122 | + private Integer id; |
| 123 | + |
| 124 | + @OneToMany |
| 125 | + @JoinColumn( name = "reference" ) |
| 126 | + private List<EntityA> entities = new ArrayList<>(); |
| 127 | + } |
| 128 | + |
| 129 | + @Entity( name = "EntityBContainer" ) |
| 130 | + @Table( name = "b_container" ) |
| 131 | + static class EntityBContainer { |
| 132 | + @Id |
| 133 | + private Integer id; |
| 134 | + |
| 135 | + @OneToMany |
| 136 | + @JoinColumn( name = "reference" ) |
| 137 | + private List<EntityB> entities = new ArrayList<>(); |
| 138 | + } |
| 139 | +} |
0 commit comments