From 840ec40f02defcf586e7b36970f7a6651bfdd7cc Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 28 Aug 2023 20:53:18 +0200 Subject: [PATCH] Revert HHH-16616 backport --- .../collection/OneToManyPersister.java | 16 +- .../entity/AbstractEntityPersister.java | 3 +- .../sqm/sql/BaseSqmToSqlAstConverter.java | 5 - .../sql/ast/spi/SqlAstCreationState.java | 5 - .../ast/tree/from/NamedTableReference.java | 2 +- .../OneToManyJoinFetchDiscriminatorTest.java | 22 +-- .../ManyToOneInheritanceSubTypeTest.java | 149 ------------------ 7 files changed, 9 insertions(+), 193 deletions(-) delete mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/ManyToOneInheritanceSubTypeTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java index 742190207c..e909790090 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java @@ -300,16 +300,12 @@ public class OneToManyPersister extends AbstractCollectionPersister { SqlAstCreationState astCreationState) { super.applyWhereFragments( predicateConsumer, alias, tableGroup, astCreationState ); - if ( !astCreationState.supportsEntityNameUsage() ) { - // We only need to apply discriminator for loads, since queries with joined - // inheritance subtypes are already filtered by the entity name usage logic - getElementPersisterInternal().applyDiscriminator( - predicateConsumer, - alias, - tableGroup, - astCreationState - ); - } + getElementPersisterInternal().applyDiscriminator( + predicateConsumer, + alias, + tableGroup, + astCreationState + ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index d4363de54a..1bd3247016 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -14,7 +14,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.IdentityHashMap; @@ -3014,7 +3013,7 @@ public abstract class AbstractEntityPersister TableGroup tableGroup, SqlAstCreationState creationState) { if ( needsDiscriminator() ) { - pruneForSubclasses( tableGroup, Collections.singletonMap( getEntityName(), EntityNameUse.TREAT ) ); + predicateConsumer.accept( createDiscriminatorPredicate( alias, tableGroup, creationState ) ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index 467bdf3a71..6e9ae8194a 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -3042,11 +3042,6 @@ public abstract class BaseSqmToSqlAstConverter extends Base } } - @Override - public boolean supportsEntityNameUsage() { - return true; - } - @Override public void registerEntityNameUsage( TableGroup tableGroup, diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationState.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationState.java index 5a599ad10e..89a46ff154 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationState.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAstCreationState.java @@ -42,9 +42,4 @@ public interface SqlAstCreationState { String hibernateEntityName) { // No-op } - - @Internal - default boolean supportsEntityNameUsage() { - return false; - } } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/NamedTableReference.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/NamedTableReference.java index d78c1a3c7e..33e4dc2f62 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/NamedTableReference.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/tree/from/NamedTableReference.java @@ -72,7 +72,7 @@ public class NamedTableReference extends AbstractTableReference { @Override public boolean containsAffectedTableName(String requestedName) { - return isEmpty( requestedName ) || getTableExpression().contains( requestedName ); + return isEmpty( requestedName ) || getTableExpression().equals( requestedName ); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/associations/OneToManyJoinFetchDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/associations/OneToManyJoinFetchDiscriminatorTest.java index 0ddc414eb8..565864589a 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/associations/OneToManyJoinFetchDiscriminatorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/inheritance/discriminator/associations/OneToManyJoinFetchDiscriminatorTest.java @@ -44,8 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @DomainModel(annotatedClasses = { OneToManyJoinFetchDiscriminatorTest.Person.class, OneToManyJoinFetchDiscriminatorTest.BodyPart.class, - OneToManyJoinFetchDiscriminatorTest.Leg.class, - OneToManyJoinFetchDiscriminatorTest.Arm.class, + OneToManyJoinFetchDiscriminatorTest.Leg.class }) @JiraKey("HHH-16157") public class OneToManyJoinFetchDiscriminatorTest { @@ -137,23 +136,4 @@ public class OneToManyJoinFetchDiscriminatorTest { return person; } } - - @Entity(name = "Arm") - @DiscriminatorValue("ArmBodyPart") - public static class Arm extends BodyPart { - @ManyToOne(fetch = FetchType.LAZY, optional = false) - private Person person; - - public Arm() { - } - - public Arm(String name, Person person) { - this.name = name; - this.person = person; - } - - public Person getPerson() { - return person; - } - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/ManyToOneInheritanceSubTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/ManyToOneInheritanceSubTypeTest.java deleted file mode 100644 index 6eff309bae..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/ManyToOneInheritanceSubTypeTest.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * 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.mapping.inheritance; - -import java.util.ArrayList; -import java.util.List; - -import org.hibernate.testing.jdbc.SQLStatementInspector; -import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.Jira; -import org.hibernate.testing.orm.junit.SessionFactory; -import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import jakarta.persistence.DiscriminatorColumn; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.Inheritance; -import jakarta.persistence.InheritanceType; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.OneToMany; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Laurent Almeras - */ -@DomainModel( annotatedClasses = { - ManyToOneInheritanceSubTypeTest.SuperType.class, - ManyToOneInheritanceSubTypeTest.TypeA.class, - ManyToOneInheritanceSubTypeTest.TypeB.class, - ManyToOneInheritanceSubTypeTest.LinkedEntity.class -} ) -@SessionFactory( useCollectingStatementInspector = true ) -@Jira( "https://hibernate.atlassian.net/browse/HHH-16616" ) -public class ManyToOneInheritanceSubTypeTest { - @BeforeAll - public void setUp(SessionFactoryScope scope) { - scope.inTransaction( session -> { - final SuperType superType = new SuperType( 1 ); - final TypeB typeB = new TypeB( 2, "typeB" ); - final TypeA typeA = new TypeA( 3, "typeA" ); - final LinkedEntity entity = new LinkedEntity( 4 ); - entity.addTypeA( typeA ); - session.persist( superType ); - session.persist( typeB ); - session.persist( typeA ); - session.persist( entity ); - } ); - } - - @Test - public void testFind(SessionFactoryScope scope) { - final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); - inspector.clear(); - scope.inTransaction( session -> { - final LinkedEntity entity = session.find( LinkedEntity.class, 4 ); - assertThat( entity.getTypeAS() ).hasSize( 1 ); - inspector.assertExecutedCount( 2 ); - inspector.assertNumberOfOccurrenceInQueryNoSpace( 1, "disc_col", 1 ); - } ); - } - - @Test - public void testJoinFetch(SessionFactoryScope scope) { - final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); - inspector.clear(); - scope.inTransaction( session -> { - final LinkedEntity entity = session.createQuery( - "from LinkedEntity e left join fetch e.typeAS", - LinkedEntity.class - ).getSingleResult(); - assertThat( entity.getTypeAS() ).hasSize( 1 ); - inspector.assertExecutedCount( 1 ); - inspector.assertNumberOfOccurrenceInQueryNoSpace( 0, "disc_col", 1 ); - } ); - } - - @Entity( name = "SuperType" ) - @Inheritance( strategy = InheritanceType.SINGLE_TABLE ) - @DiscriminatorColumn( name = "disc_col" ) - public static class SuperType { - @Id - private Integer id; - - public SuperType() { - } - - public SuperType(Integer id) { - this.id = id; - } - } - - @Entity( name = "TypeA" ) - public static class TypeA extends SuperType { - private String typeAName; - - public TypeA() { - } - - public TypeA(Integer id, String typeAName) { - super( id ); - this.typeAName = typeAName; - } - } - - @Entity( name = "TypeB" ) - public static class TypeB extends SuperType { - private String typeBName; - - public TypeB() { - } - - public TypeB(Integer id, String typeBName) { - super( id ); - this.typeBName = typeBName; - } - } - - @Entity( name = "LinkedEntity" ) - public static class LinkedEntity { - @Id - private Integer id; - - @OneToMany - @JoinColumn( name = "linked_id" ) - private List typeAS = new ArrayList<>(); - - public LinkedEntity() { - } - - public LinkedEntity(Integer id) { - this.id = id; - } - - public List getTypeAS() { - return typeAS; - } - - public void addTypeA(TypeA typeA) { - this.typeAS.add( typeA ); - } - } -}