From 0ecd66fd466478210796cc156be0edd5eaaef39c Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 7 May 2021 12:48:06 +0200 Subject: [PATCH] Fix use EmbeddedIdentifier instance as Entity --- .../internal/EmbeddableAssembler.java | 12 ---- .../entity/AbstractEntityInitializer.java | 56 +++++++++---------- .../orm/test/onetoone/basic/Mapping.hbm.xml | 25 --------- .../onetoone/formula/OneToOneFormulaTest.java | 10 ++++ 4 files changed, 36 insertions(+), 67 deletions(-) delete mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/basic/Mapping.hbm.xml diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/EmbeddableAssembler.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/EmbeddableAssembler.java index 111318b525..f308200bdd 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/EmbeddableAssembler.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/embeddable/internal/EmbeddableAssembler.java @@ -18,17 +18,9 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor; */ public class EmbeddableAssembler implements DomainResultAssembler { protected final EmbeddableInitializer initializer; - private final boolean containingClass; public EmbeddableAssembler(EmbeddableInitializer initializer) { this.initializer = initializer; - if ( initializer instanceof AbstractCompositeIdentifierMapping ) { - containingClass = ( (AbstractCompositeIdentifierMapping) initializer.getInitializedPart() ) - .hasContainingClass(); - } - else { - containingClass = true; - } } @Override @@ -36,10 +28,6 @@ public class EmbeddableAssembler implements DomainResultAssembler { return initializer.getInitializedPart().getJavaTypeDescriptor(); } - public boolean hasContainingClass() { - return containingClass; - } - @Override public Object assemble(RowProcessingState rowProcessingState, JdbcValuesSourceProcessingOptions options) { initializer.resolveKey( rowProcessingState ); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java index 9ccbca00ee..a0f9fe05c5 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/AbstractEntityInitializer.java @@ -74,7 +74,6 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces private final List identifierInitializers = new ArrayList<>(); private final DomainResultAssembler identifierAssembler; - private final boolean embeddableIdentifierWithNoContainingClass; private final DomainResultAssembler discriminatorAssembler; private final DomainResultAssembler versionAssembler; private final DomainResultAssembler rowIdAssembler; @@ -161,8 +160,6 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces this.identifierAssembler = null; } - embeddableIdentifierWithNoContainingClass = hasEmbeddableIdentifierWithNoContainingClass( identifierAssembler ); - if ( discriminatorResult != null ) { discriminatorAssembler = discriminatorResult.createResultAssembler( creationState ); } @@ -214,13 +211,6 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces ); } - protected final boolean hasEmbeddableIdentifierWithNoContainingClass(DomainResultAssembler identifierAssembler) { - if ( identifierAssembler instanceof EmbeddableAssembler ) { - return !( (EmbeddableAssembler) identifierAssembler ).hasContainingClass(); - } - return false; - } - @Override public ModelPart getInitializedPart() { return referencedModelPart; @@ -369,18 +359,8 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces final JdbcValuesSourceProcessingState jdbcValuesSourceProcessingState = rowProcessingState.getJdbcValuesSourceProcessingState(); final SharedSessionContractImplementor session = jdbcValuesSourceProcessingState.getSession(); - Object id; // 1) resolve the hydrated identifier value(s) into its identifier representation - if ( identifierAssembler == null ) { - id = jdbcValuesSourceProcessingState.getProcessingOptions().getEffectiveOptionalId(); - } - else { - initializeIdentifier( rowProcessingState ); - id = identifierAssembler.assemble( - rowProcessingState, - jdbcValuesSourceProcessingState.getProcessingOptions() - ); - } + final Object id = initializeIdentifier( rowProcessingState, jdbcValuesSourceProcessingState ); if ( id == null ) { missing = true; @@ -388,10 +368,6 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces return; } - if ( embeddableIdentifierWithNoContainingClass ) { - entityInstance = id; - } - // 2) build the EntityKey this.entityKey = new EntityKey( id, concreteDescriptor ); @@ -407,6 +383,28 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces } } + private Object initializeIdentifier( + RowProcessingState rowProcessingState, + JdbcValuesSourceProcessingState jdbcValuesSourceProcessingState) { + final Object id = jdbcValuesSourceProcessingState.getProcessingOptions().getEffectiveOptionalId(); + final boolean useEmbeddedIdentifierInstanceAsEntity = id != null && id.getClass() + .equals( concreteDescriptor.getJavaTypeDescriptor().getJavaType() ); + if ( useEmbeddedIdentifierInstanceAsEntity ) { + entityInstance = id; + return id; + } + + if ( identifierAssembler == null ) { + return id; + } + + initializeIdentifier( rowProcessingState ); + return identifierAssembler.assemble( + rowProcessingState, + jdbcValuesSourceProcessingState.getProcessingOptions() + ); + } + @SuppressWarnings("WeakerAccess") protected void initializeIdentifier(RowProcessingState rowProcessingState) { if ( EntityLoadingLogger.TRACE_ENABLED ) { @@ -417,11 +415,9 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces ); } - if ( !embeddableIdentifierWithNoContainingClass ) { - identifierInitializers.forEach( initializer -> initializer.resolveKey( rowProcessingState ) ); - identifierInitializers.forEach( initializer -> initializer.resolveInstance( rowProcessingState ) ); - identifierInitializers.forEach( initializer -> initializer.initializeInstance( rowProcessingState ) ); - } + identifierInitializers.forEach( initializer -> initializer.resolveKey( rowProcessingState ) ); + identifierInitializers.forEach( initializer -> initializer.resolveInstance( rowProcessingState ) ); + identifierInitializers.forEach( initializer -> initializer.initializeInstance( rowProcessingState ) ); if ( EntityLoadingLogger.TRACE_ENABLED ) { EntityLoadingLogger.LOGGER.tracef( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/basic/Mapping.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/basic/Mapping.hbm.xml deleted file mode 100644 index 11aa0f9bf7..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/basic/Mapping.hbm.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/formula/OneToOneFormulaTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/formula/OneToOneFormulaTest.java index 85eebd08ef..411776155b 100755 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/formula/OneToOneFormulaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetoone/formula/OneToOneFormulaTest.java @@ -163,6 +163,16 @@ public class OneToOneFormulaTest extends BaseSessionFactoryFunctionalTest { assertNull( p.getMailingAddress() ); } ); + + inTransaction( session -> { + Address a = (Address) session.createQuery( "from Address" ).uniqueResult(); + + Person person = a.getPerson(); + assertNotNull( person ); + assertTrue( Hibernate.isInitialized( person.getAddress() ) ); + assertTrue( Hibernate.isInitialized( person.getMailingAddress() ) ); + assertNull( person.getMailingAddress() ); + } ); }