From f2b4454c0e971d763d819d071b9f892ad0ceb6bb Mon Sep 17 00:00:00 2001 From: The-Huginn Date: Wed, 2 Aug 2023 15:28:27 +0200 Subject: [PATCH] [HHH-16979] Change SqmPathSource resolution method call --- .../NonAggregatedCompositeSimplePath.java | 2 +- .../tree/domain/SqmAnyValuedSimplePath.java | 2 +- .../tree/domain/SqmBasicValuedSimplePath.java | 2 +- .../domain/SqmEmbeddedValuedSimplePath.java | 2 +- .../domain/SqmEntityValuedSimplePath.java | 2 +- .../domain/SqmPluralValuedSimplePath.java | 7 +- .../JoinedInheritanceImplicitJoinTest.java | 181 ++++++++++++++++++ 7 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/persister/entity/JoinedInheritanceImplicitJoinTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/NonAggregatedCompositeSimplePath.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/NonAggregatedCompositeSimplePath.java index 25b4fc8197..38e40b5262 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/NonAggregatedCompositeSimplePath.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/NonAggregatedCompositeSimplePath.java @@ -44,7 +44,7 @@ public class NonAggregatedCompositeSimplePath extends SqmEntityValuedSimplePa this, new NonAggregatedCompositeSimplePath<>( getNavigablePathCopy( lhsCopy ), - getReferencedPathSource(), + getModel(), lhsCopy, nodeBuilder() ) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmAnyValuedSimplePath.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmAnyValuedSimplePath.java index 1b1cae7888..a2997188a8 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmAnyValuedSimplePath.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmAnyValuedSimplePath.java @@ -54,7 +54,7 @@ public class SqmAnyValuedSimplePath extends AbstractSqmSimplePath { this, new SqmAnyValuedSimplePath<>( getNavigablePathCopy( lhsCopy ), - getReferencedPathSource(), + getModel(), lhsCopy, getExplicitAlias(), nodeBuilder() diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmBasicValuedSimplePath.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmBasicValuedSimplePath.java index ea02122525..bb7e62bf81 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmBasicValuedSimplePath.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmBasicValuedSimplePath.java @@ -55,7 +55,7 @@ public class SqmBasicValuedSimplePath this, new SqmBasicValuedSimplePath<>( getNavigablePathCopy( lhsCopy ), - getReferencedPathSource(), + getModel(), lhsCopy, getExplicitAlias(), nodeBuilder() diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmEmbeddedValuedSimplePath.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmEmbeddedValuedSimplePath.java index c22241e218..c7801538ac 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmEmbeddedValuedSimplePath.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmEmbeddedValuedSimplePath.java @@ -64,7 +64,7 @@ public class SqmEmbeddedValuedSimplePath this, new SqmEmbeddedValuedSimplePath<>( getNavigablePathCopy( lhsCopy ), - getReferencedPathSource(), + getModel(), lhsCopy, getExplicitAlias(), nodeBuilder() diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmEntityValuedSimplePath.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmEntityValuedSimplePath.java index bdc7476475..b849db29ea 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmEntityValuedSimplePath.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmEntityValuedSimplePath.java @@ -39,7 +39,7 @@ public class SqmEntityValuedSimplePath extends AbstractSqmSimplePath { this, new SqmEntityValuedSimplePath<>( getNavigablePathCopy( lhsCopy ), - getReferencedPathSource(), + getModel(), lhsCopy, nodeBuilder() ) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmPluralValuedSimplePath.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmPluralValuedSimplePath.java index 03c2a3001d..2b49aa819f 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmPluralValuedSimplePath.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/domain/SqmPluralValuedSimplePath.java @@ -61,7 +61,7 @@ public class SqmPluralValuedSimplePath extends AbstractSqmSimplePath { this, new SqmPluralValuedSimplePath<>( getNavigablePathCopy( lhsCopy ), - getReferencedPathSource(), + getModel(), lhsCopy, getExplicitAlias(), nodeBuilder() @@ -76,6 +76,11 @@ public class SqmPluralValuedSimplePath extends AbstractSqmSimplePath { return (PluralPersistentAttribute) super.getReferencedPathSource(); } + @Override + public PluralPersistentAttribute getModel() { + return (PluralPersistentAttribute) super.getModel(); + } + @Override public PluralPersistentAttribute getNodeType() { return getReferencedPathSource(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/persister/entity/JoinedInheritanceImplicitJoinTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/persister/entity/JoinedInheritanceImplicitJoinTest.java new file mode 100644 index 0000000000..5efadb712e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/persister/entity/JoinedInheritanceImplicitJoinTest.java @@ -0,0 +1,181 @@ +package org.hibernate.orm.test.persister.entity; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Tuple; +import jakarta.persistence.TypedQuery; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Join; +import jakarta.persistence.criteria.Root; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Set; + +@TestForIssue(jiraKey = "HHH-16979") +@Jpa(annotatedClasses = { + JoinedInheritanceImplicitJoinTest.MasterEntity.class, + JoinedInheritanceImplicitJoinTest.ChildEntity.class, + JoinedInheritanceImplicitJoinTest.RelatedEntity.class +}, properties = { + @Setting(name = AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, value = "true") +}) +public class JoinedInheritanceImplicitJoinTest { + + @BeforeEach + public void setup(EntityManagerFactoryScope scope) { + scope.inTransaction(entityManager -> { + final var childEntity = new ChildEntity(); + childEntity.setMasterField("masterValue001"); + childEntity.setChildField("childValue001"); + + RelatedEntity relatedEntity = new RelatedEntity(); + relatedEntity.setRelatedField("relatedValue001"); + relatedEntity.setChildEntity(childEntity); + + RelatedEntity relatedEntity2 = new RelatedEntity(); + relatedEntity2.setRelatedField("relatedValue002"); + relatedEntity2.setChildEntity(childEntity); + + Set relatedEntityHashSet = Set.of(relatedEntity, relatedEntity2); + + childEntity.setRelatedEntitySet(relatedEntityHashSet); + + entityManager.persist(childEntity); + entityManager.persist(relatedEntity); + entityManager.persist(relatedEntity2); + }); + } + + @Test + public void testImplicitJoinWithoutRelatedEntity(EntityManagerFactoryScope scope) { + scope.inTransaction(entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery(Tuple.class); + Root root = query.from(ChildEntity.class); + + query.multiselect( + root.get("masterField"), + root.get("childField") + ); + + TypedQuery result = entityManager.createQuery(query); + List resultList = result.getResultList(); + + Assertions.assertFalse(resultList.isEmpty()); + }); + } + + @Test + public void testImplicitJoinWithRelatedEntity(EntityManagerFactoryScope scope) { + scope.inTransaction(entityManager -> { + CriteriaBuilder cb = entityManager.getCriteriaBuilder(); + CriteriaQuery query = cb.createQuery(Tuple.class); + Root root = query.from(ChildEntity.class); + Join relatedEntityJoin = root.join("relatedEntitySet"); + + query.multiselect( + root.get("masterField"), + root.get("childField"), + relatedEntityJoin.get("relatedField") + ); + + TypedQuery result = entityManager.createQuery(query); + List resultList = result.getResultList(); + + Assertions.assertFalse(resultList.isEmpty()); + }); + } + + @Entity + @Inheritance(strategy = InheritanceType.JOINED) + public class MasterEntity { + @Id + @GeneratedValue + public Long id; + + public String masterField; + + + public Long getId() { + return id; + } + + public String getMasterField() { + return masterField; + } + + public void setMasterField(final String masterField) { + this.masterField = masterField; + } + } + + @Entity + public class ChildEntity extends MasterEntity { + private String childField; + + @OneToMany(mappedBy = "childEntity") + public Set relatedEntitySet; + + public String getChildField() { + return childField; + } + + public void setChildField(final String childField) { + this.childField = childField; + } + + public Set getRelatedEntitySet() { + return relatedEntitySet; + } + + public void setRelatedEntitySet(final Set relatedEntitySet) { + this.relatedEntitySet = relatedEntitySet; + } + } + + @Entity + public class RelatedEntity { + @Id + @GeneratedValue + public Long id; + + public String relatedField; + + @ManyToOne + public ChildEntity childEntity; + + public Long getId() { + return id; + } + + public String getRelatedField() { + return relatedField; + } + + public void setRelatedField(final String relatedField) { + this.relatedField = relatedField; + } + + public ChildEntity getChildEntity() { + return childEntity; + } + + public void setChildEntity(final ChildEntity childEntity) { + this.childEntity = childEntity; + } + } +}