From 9f155a95ab00d68a2d819fc75912be85f96ca0ef Mon Sep 17 00:00:00 2001 From: Fabio Massimo Ercoli Date: Wed, 17 Nov 2021 11:20:39 +0100 Subject: [PATCH] Fix EnhancedProxy with Embeddable and Lazy BasicAttribute --- .../internal/MappingModelCreationHelper.java | 13 +- .../inheritance/BaseIdEntityByteCodeTest.java | 122 ++++++++++++++++++ 2 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/BaseIdEntityByteCodeTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java index 136dc5539b..45c92ffe2e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java @@ -53,6 +53,7 @@ import org.hibernate.metamodel.mapping.CollectionIdentifierDescriptor; import org.hibernate.metamodel.mapping.CollectionMappingType; import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.CompositeIdentifierMapping; +import org.hibernate.metamodel.mapping.EmbeddableMappingType; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.EntityIdentifierMapping; import org.hibernate.metamodel.mapping.EntityMappingType; @@ -232,8 +233,16 @@ public class MappingModelCreationHelper { } }; - final FetchTiming fetchTiming = bootProperty.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE; - final FetchStyle fetchStyle = bootProperty.isLazy() ? FetchStyle.SELECT : FetchStyle.JOIN; + final FetchTiming fetchTiming; + final FetchStyle fetchStyle; + if ( declaringType instanceof EmbeddableMappingType ) { + fetchTiming = FetchTiming.IMMEDIATE; + fetchStyle = FetchStyle.JOIN; + } + else { + fetchTiming = bootProperty.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE; + fetchStyle = bootProperty.isLazy() ? FetchStyle.SELECT : FetchStyle.JOIN; + } final ValueGeneration valueGeneration = bootProperty.getValueGenerationStrategy(); if ( valueConverter != null ) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/BaseIdEntityByteCodeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/BaseIdEntityByteCodeTest.java new file mode 100644 index 0000000000..fae6d106ea --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/BaseIdEntityByteCodeTest.java @@ -0,0 +1,122 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.mapping.inheritance; + +import org.hibernate.annotations.LazyGroup; + +import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; +import org.junit.runner.RunWith; + +import jakarta.persistence.Basic; +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(BytecodeEnhancerRunner.class) +public class BaseIdEntityByteCodeTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { BaseEntity.class, ContainingEntity.class }; + } + + @Test + public void test() { + inTransaction( session -> { + ContainingEntity entity1 = new ContainingEntity(); + entity1.id = 1; + entity1.baseText = "initialValue"; + + ContainedEmbeddable containedEmbeddable = new ContainedEmbeddable(); + entity1.containedEmbeddable = containedEmbeddable; + containedEmbeddable.text = "initialValue"; + session.persist( entity1 ); + } ); + + inTransaction( session -> { + ContainingEntity entity = session.load( ContainingEntity.class, 1 ); + ContainedEmbeddable containedEmbeddable = entity.getContainedEmbeddable(); + assertThat( containedEmbeddable.getText() ).isEqualTo( "initialValue" ); + } ); + } + + @Entity(name = "base") + public static class BaseEntity { + + @Id + public Integer id; + + @Basic(fetch = FetchType.LAZY) + @LazyGroup("base") + public String baseText; + + public String baseText2; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getBaseText() { + return baseText; + } + + public void setBaseText(String baseText) { + this.baseText = baseText; + } + + public String getBaseText2() { + return baseText2; + } + + public void setBaseText2(String baseText2) { + this.baseText2 = baseText2; + } + + + } + + @Entity(name = "containing") + public static class ContainingEntity extends BaseEntity { + @Embedded + public ContainedEmbeddable containedEmbeddable; + + public ContainedEmbeddable getContainedEmbeddable() { + return containedEmbeddable; + } + + public void setContainedEmbeddable(ContainedEmbeddable containedEmbeddable) { + this.containedEmbeddable = containedEmbeddable; + } + + } + + @Embeddable + public static class ContainedEmbeddable { + + @Basic(fetch = FetchType.LAZY) + public String text; + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + } + +}