Fix EnhancedProxy with Embeddable and Lazy BasicAttribute

This commit is contained in:
Fabio Massimo Ercoli 2021-11-17 11:20:39 +01:00 committed by Steve Ebersole
parent ae25b651ca
commit 9f155a95ab
2 changed files with 133 additions and 2 deletions

View File

@ -53,6 +53,7 @@ import org.hibernate.metamodel.mapping.CollectionIdentifierDescriptor;
import org.hibernate.metamodel.mapping.CollectionMappingType; import org.hibernate.metamodel.mapping.CollectionMappingType;
import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.CompositeIdentifierMapping; import org.hibernate.metamodel.mapping.CompositeIdentifierMapping;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart; import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping; import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.EntityMappingType;
@ -232,8 +233,16 @@ public class MappingModelCreationHelper {
} }
}; };
final FetchTiming fetchTiming = bootProperty.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE; final FetchTiming fetchTiming;
final FetchStyle fetchStyle = bootProperty.isLazy() ? FetchStyle.SELECT : FetchStyle.JOIN; 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(); final ValueGeneration valueGeneration = bootProperty.getValueGenerationStrategy();
if ( valueConverter != null ) { if ( valueConverter != null ) {

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}