diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java index ee883cea48..8da47807a1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/EntityIdentifierMapping.java @@ -8,7 +8,6 @@ package org.hibernate.metamodel.mapping; import org.hibernate.engine.spi.IdentifierValue; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.persister.entity.EntityPersister; /** @@ -43,15 +42,7 @@ public interface EntityIdentifierMapping extends ValueMapping, ModelPart { Object getIdentifier(Object entity); - /** - * @deprecated use {@link #setIdentifier(Object, Object, EntityPersister, SharedSessionContractImplementor)} instead. - */ - @Deprecated void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session); - default void setIdentifier(Object entity, Object id, EntityPersister entityDescriptor, SharedSessionContractImplementor session){ - setIdentifier( entity, id, session ); - } - Object instantiate(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/InverseNonAggregatedIdentifierMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/InverseNonAggregatedIdentifierMapping.java index 22a0457bb7..98783f5d77 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/InverseNonAggregatedIdentifierMapping.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/InverseNonAggregatedIdentifierMapping.java @@ -11,20 +11,20 @@ import java.util.function.BiConsumer; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.PersistenceContext; -import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.metamodel.mapping.AttributeMapping; import org.hibernate.metamodel.mapping.EmbeddableMappingType; import org.hibernate.metamodel.mapping.EntityIdentifierMapping; +import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.ManagedMappingType; import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping; import org.hibernate.metamodel.mapping.SelectableMappings; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.spi.NavigablePath; import org.hibernate.query.sqm.sql.SqmToSqlAstConverter; +import org.hibernate.spi.NavigablePath; import org.hibernate.sql.ast.Clause; import org.hibernate.sql.ast.spi.SqlAstCreationState; import org.hibernate.sql.ast.spi.SqlExpressionResolver; @@ -44,6 +44,7 @@ import org.hibernate.sql.results.graph.DomainResultCreationState; */ public class InverseNonAggregatedIdentifierMapping extends EmbeddedAttributeMapping implements NonAggregatedIdentifierMapping { private final IdClassEmbeddable idClassEmbeddable; + private final EntityMappingType entityDescriptor; private final NonAggregatedIdentifierMapping.IdentifierValueMapper identifierValueMapper; @@ -63,6 +64,9 @@ public class InverseNonAggregatedIdentifierMapping extends EmbeddedAttributeMapp embeddableTypeDescriptor, creationProcess ); + + this.entityDescriptor = inverseModelPart.findContainingEntityMapping(); + if ( inverseModelPart.getIdClassEmbeddable() == null ) { this.idClassEmbeddable = null; this.identifierValueMapper = (NonAggregatedIdentifierMapping.IdentifierValueMapper) super.getEmbeddableTypeDescriptor(); @@ -236,14 +240,6 @@ public class InverseNonAggregatedIdentifierMapping extends EmbeddedAttributeMapp @Override public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) { - final EntityPersister entityDescriptor = session.getFactory().getRuntimeMetamodels() - .getMappingMetamodel() - .getEntityDescriptor( entity.getClass() ); - setIdentifier( entity, id, entityDescriptor, session ); - } - - @Override - public void setIdentifier(Object entity, Object id, EntityPersister entityDescriptor, SharedSessionContractImplementor session) { final List mappedIdAttributeMappings = identifierValueMapper.getAttributeMappings(); final Object[] propertyValues = new Object[mappedIdAttributeMappings.size()]; @@ -263,11 +259,11 @@ public class InverseNonAggregatedIdentifierMapping extends EmbeddedAttributeMapp // otherwise look for an initialized version propertyValues[position] = persistenceContext.getEntity( entityKey ); if ( propertyValues[position] == null ) { - // get the association out of the entity itself - propertyValues[position] = entityDescriptor.getPropertyValue( - entity, - toOneAttributeMapping.getAttributeName() - ); + propertyValues[position] = entityDescriptor + .findAttributeMapping( toOneAttributeMapping.getAttributeName() ) + .getPropertyAccess() + .getGetter() + .get( entity ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NonAggregatedIdentifierMappingImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NonAggregatedIdentifierMappingImpl.java index d4756d56aa..b30dbe2b78 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NonAggregatedIdentifierMappingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NonAggregatedIdentifierMappingImpl.java @@ -11,7 +11,6 @@ import java.util.function.BiConsumer; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.PersistenceContext; -import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.mapping.Component; @@ -25,8 +24,8 @@ import org.hibernate.metamodel.mapping.ModelPart; import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping; import org.hibernate.metamodel.mapping.SelectableMappings; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.spi.NavigablePath; import org.hibernate.query.sqm.sql.SqmToSqlAstConverter; +import org.hibernate.spi.NavigablePath; import org.hibernate.sql.ast.Clause; import org.hibernate.sql.ast.spi.SqlAstCreationState; import org.hibernate.sql.ast.spi.SqlExpressionResolver; @@ -47,6 +46,8 @@ import org.hibernate.sql.results.graph.DomainResultCreationState; * Can also be a single {@link jakarta.persistence.Id} with {@link jakarta.persistence.MapsId} */ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentifierMapping implements NonAggregatedIdentifierMapping { + private final EntityPersister entityDescriptor; + private final VirtualIdEmbeddable virtualIdEmbeddable; private final IdClassEmbeddable idClassEmbeddable; @@ -59,6 +60,8 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif String[] rootTableKeyColumnNames, MappingModelCreationProcess creationProcess) { super( entityPersister, rootTableName, creationProcess ); + entityDescriptor = entityPersister; + if ( bootEntityDescriptor.getIdentifierMapper() == null || bootEntityDescriptor.getIdentifierMapper() == bootEntityDescriptor.getIdentifier() ) { // cid -> getIdentifier @@ -104,6 +107,13 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif } } + /** + * The entity whose identifier this mapping is the inverse of + */ + public EntityPersister getIdentifiedEntityDescriptor() { + return entityDescriptor; + } + @Override public EmbeddableMappingType getMappedType() { return virtualIdEmbeddable; @@ -256,14 +266,6 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif @Override public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) { - final EntityPersister entityDescriptor = session.getFactory().getRuntimeMetamodels() - .getMappingMetamodel() - .getEntityDescriptor( entity.getClass() ); - setIdentifier( entity, id, entityDescriptor, session ); - } - - @Override - public void setIdentifier(Object entity, Object id, EntityPersister entityDescriptor, SharedSessionContractImplementor session) { final List mappedIdAttributeMappings = identifierValueMapper.getAttributeMappings(); final Object[] propertyValues = new Object[mappedIdAttributeMappings.size()]; 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 91f3aab361..0f0f48edde 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 @@ -5191,7 +5191,7 @@ public abstract class AbstractEntityPersister @Override public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) { - identifierMapping.setIdentifier( entity, id, this, session ); + identifierMapping.setIdentifier( entity, id, session ); } @Override