HHH-18131 Composite identifiers with associations stopped working with @IdClass
This commit is contained in:
parent
3aae473eca
commit
80bbea4283
|
@ -164,7 +164,7 @@ public class DefaultMergeEventListener
|
|||
final Object originalId;
|
||||
if ( entry == null ) {
|
||||
final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
|
||||
originalId = persister.getIdentifier( entity, source );
|
||||
originalId = persister.getIdentifier( entity, copiedAlready );
|
||||
if ( originalId != null ) {
|
||||
final EntityKey entityKey;
|
||||
if ( persister.getIdentifierType() instanceof ComponentType ) {
|
||||
|
|
|
@ -366,4 +366,8 @@ public class MergeContext implements Map<Object,Object> {
|
|||
// Entity was not found in current persistence context. Use Object#toString() method.
|
||||
return "[" + entity + "]";
|
||||
}
|
||||
|
||||
public EventSource getEventSource() {
|
||||
return session;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
|
|||
import org.hibernate.engine.spi.EntityHolder;
|
||||
import org.hibernate.engine.spi.EntityKey;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.engine.spi.TypedValue;
|
||||
import org.hibernate.internal.CoreLogging;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
|
@ -53,7 +54,7 @@ public final class EntityPrinter {
|
|||
result.put(
|
||||
entityPersister.getIdentifierPropertyName(),
|
||||
entityPersister.getIdentifierType().toLoggableString(
|
||||
entityPersister.getIdentifier( entity, null ),
|
||||
entityPersister.getIdentifier( entity, (SharedSessionContractImplementor) null ),
|
||||
factory
|
||||
)
|
||||
);
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.hibernate.Hibernate;
|
|||
import org.hibernate.MappingException;
|
||||
import org.hibernate.engine.spi.EntityEntry;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.jpa.internal.util.PersistenceUtilHelper;
|
||||
import org.hibernate.persister.entity.EntityPersister;
|
||||
import org.hibernate.proxy.LazyInitializer;
|
||||
|
@ -106,7 +107,7 @@ public class PersistenceUnitUtilImpl implements PersistenceUnitUtil, Serializabl
|
|||
catch (MappingException ex) {
|
||||
throw new IllegalArgumentException( entityClass.getName() + " is not an entity", ex );
|
||||
}
|
||||
return persister.getIdentifier( entity, null );
|
||||
return persister.getIdentifier( entity, (SharedSessionContractImplementor) null );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.hibernate.TransientObjectException;
|
|||
import org.hibernate.engine.internal.ForeignKeys;
|
||||
import org.hibernate.engine.spi.IdentifierValue;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.event.spi.MergeContext;
|
||||
|
||||
import jakarta.persistence.EmbeddedId;
|
||||
import jakarta.persistence.Id;
|
||||
|
@ -68,6 +69,15 @@ public interface EntityIdentifierMapping extends ValuedModelPart {
|
|||
*/
|
||||
Object getIdentifier(Object entity);
|
||||
|
||||
/**
|
||||
* Extract the identifier from an instance of the entity
|
||||
*
|
||||
* It's supposed to be use during the merging process
|
||||
*/
|
||||
default Object getIdentifier(Object entity, MergeContext mergeContext){
|
||||
return getIdentifier( entity );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the identifier of the persistent or transient object, or throw
|
||||
* an exception if the instance is "unsaved"
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.hibernate.engine.spi.EntityHolder;
|
|||
import org.hibernate.engine.spi.EntityKey;
|
||||
import org.hibernate.engine.spi.PersistenceContext;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.event.spi.MergeContext;
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
import org.hibernate.metamodel.mapping.AttributeMapping;
|
||||
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
|
||||
|
@ -196,6 +197,11 @@ public class InverseNonAggregatedIdentifierMapping extends EmbeddedAttributeMapp
|
|||
|
||||
@Override
|
||||
public Object getIdentifier(Object entity) {
|
||||
return getIdentifier( entity, null );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getIdentifier(Object entity, MergeContext mergeContext) {
|
||||
if ( hasContainingClass() ) {
|
||||
final Object id = identifierValueMapper.getRepresentationStrategy().getInstantiator().instantiate(
|
||||
null,
|
||||
|
@ -218,16 +224,17 @@ public class InverseNonAggregatedIdentifierMapping extends EmbeddedAttributeMapp
|
|||
//JPA 2 @MapsId + @IdClass points to the pk of the entity
|
||||
else if ( attributeMapping instanceof ToOneAttributeMapping
|
||||
&& !( identifierValueMapper.getAttributeMapping( i ) instanceof ToOneAttributeMapping ) ) {
|
||||
final Object toOne = getIfMerged( o, mergeContext );
|
||||
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
|
||||
final ModelPart targetPart = toOneAttributeMapping.getForeignKeyDescriptor().getPart(
|
||||
toOneAttributeMapping.getSideNature().inverse()
|
||||
);
|
||||
if ( targetPart.isEntityIdentifierMapping() ) {
|
||||
propertyValues[i] = ( (EntityIdentifierMapping) targetPart ).getIdentifier( o );
|
||||
propertyValues[i] = ( (EntityIdentifierMapping) targetPart )
|
||||
.getIdentifier( toOne, mergeContext );
|
||||
}
|
||||
else {
|
||||
propertyValues[i] = o;
|
||||
assert false;
|
||||
propertyValues[i] = toOne;
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -242,6 +249,16 @@ public class InverseNonAggregatedIdentifierMapping extends EmbeddedAttributeMapp
|
|||
}
|
||||
}
|
||||
|
||||
private static Object getIfMerged(Object o, MergeContext mergeContext) {
|
||||
if ( mergeContext != null ) {
|
||||
final Object merged = mergeContext.get( o );
|
||||
if ( merged != null ) {
|
||||
return merged;
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
|
||||
final Object[] propertyValues = new Object[identifierValueMapper.getNumberOfAttributeMappings()];
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.hibernate.engine.spi.EntityHolder;
|
|||
import org.hibernate.engine.spi.EntityKey;
|
||||
import org.hibernate.engine.spi.PersistenceContext;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.event.spi.MergeContext;
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
import org.hibernate.mapping.Component;
|
||||
import org.hibernate.mapping.RootClass;
|
||||
|
@ -237,6 +238,11 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
|
|||
|
||||
@Override
|
||||
public Object getIdentifier(Object entity) {
|
||||
return getIdentifier( entity, null );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getIdentifier(Object entity, MergeContext mergeContext) {
|
||||
if ( hasContainingClass() ) {
|
||||
final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( entity );
|
||||
if ( lazyInitializer != null ) {
|
||||
|
@ -259,16 +265,16 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
|
|||
//JPA 2 @MapsId + @IdClass points to the pk of the entity
|
||||
else if ( attributeMapping instanceof ToOneAttributeMapping
|
||||
&& !( identifierValueMapper.getAttributeMapping( i ) instanceof ToOneAttributeMapping ) ) {
|
||||
final Object toOne = getIfMerged( o, mergeContext );
|
||||
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
|
||||
final ModelPart targetPart = toOneAttributeMapping.getForeignKeyDescriptor().getPart(
|
||||
toOneAttributeMapping.getSideNature().inverse()
|
||||
);
|
||||
if ( targetPart.isEntityIdentifierMapping() ) {
|
||||
propertyValues[i] = ( (EntityIdentifierMapping) targetPart ).getIdentifier( o );
|
||||
propertyValues[i] = ( (EntityIdentifierMapping) targetPart ).getIdentifier( toOne, mergeContext );
|
||||
}
|
||||
else {
|
||||
propertyValues[i] = o;
|
||||
assert false;
|
||||
propertyValues[i] = toOne;
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -285,6 +291,16 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
|
|||
}
|
||||
}
|
||||
|
||||
private static Object getIfMerged(Object o, MergeContext mergeContext) {
|
||||
if ( mergeContext != null ) {
|
||||
final Object merged = mergeContext.get( o );
|
||||
if ( merged != null ) {
|
||||
return merged;
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
|
||||
final Object[] propertyValues = new Object[identifierValueMapper.getNumberOfAttributeMappings()];
|
||||
|
|
|
@ -100,6 +100,7 @@ import org.hibernate.engine.spi.SessionImplementor;
|
|||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.event.spi.EventSource;
|
||||
import org.hibernate.event.spi.LoadEvent;
|
||||
import org.hibernate.event.spi.MergeContext;
|
||||
import org.hibernate.generator.BeforeExecutionGenerator;
|
||||
import org.hibernate.generator.EventType;
|
||||
import org.hibernate.generator.Generator;
|
||||
|
@ -4724,6 +4725,11 @@ public abstract class AbstractEntityPersister
|
|||
return identifierMapping.getIdentifier( entity );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getIdentifier(Object entity, MergeContext mergeContext) {
|
||||
return identifierMapping.getIdentifier( entity, mergeContext );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
|
||||
identifierMapping.setIdentifier( entity, id, session );
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
|
|||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.event.spi.EventSource;
|
||||
import org.hibernate.event.spi.MergeContext;
|
||||
import org.hibernate.generator.BeforeExecutionGenerator;
|
||||
import org.hibernate.generator.EventType;
|
||||
import org.hibernate.generator.Generator;
|
||||
|
@ -1130,8 +1131,18 @@ public interface EntityPersister extends EntityMappingType, EntityMutationTarget
|
|||
*/
|
||||
Object getIdentifier(Object entity, SharedSessionContractImplementor session);
|
||||
|
||||
/**
|
||||
* Inject the identifier value into the given entity.
|
||||
/**
|
||||
* Get the identifier of an instance from the object's identifier property.
|
||||
* Throw an exception if it has no identifier property.
|
||||
*
|
||||
* It's supposed to be use during the merging process
|
||||
*/
|
||||
default Object getIdentifier(Object entity, MergeContext mergeContext) {
|
||||
return getIdentifier( entity, mergeContext.getEventSource() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject the identifier value into the given entity.
|
||||
*/
|
||||
void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session);
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ package org.hibernate.query.derived;
|
|||
import org.hibernate.Incubating;
|
||||
import org.hibernate.engine.spi.IdentifierValue;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.event.spi.MergeContext;
|
||||
import org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping;
|
||||
import org.hibernate.metamodel.mapping.JdbcMapping;
|
||||
import org.hibernate.metamodel.mapping.ManagedMappingType;
|
||||
|
@ -51,6 +52,11 @@ public class AnonymousTupleBasicEntityIdentifierMapping
|
|||
return delegate.getIdentifier( entity );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getIdentifier(Object entity, MergeContext mergeContext) {
|
||||
return delegate.getIdentifier( entity, mergeContext );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
|
||||
delegate.setIdentifier( entity, id, session );
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.Set;
|
|||
import org.hibernate.Incubating;
|
||||
import org.hibernate.engine.spi.IdentifierValue;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.event.spi.MergeContext;
|
||||
import org.hibernate.metamodel.mapping.CompositeIdentifierMapping;
|
||||
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
|
||||
import org.hibernate.metamodel.mapping.internal.SingleAttributeIdentifierMapping;
|
||||
|
@ -70,6 +71,11 @@ public class AnonymousTupleEmbeddedEntityIdentifierMapping extends AnonymousTupl
|
|||
return delegate.getIdentifier( entity );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getIdentifier(Object entity, MergeContext mergeContext) {
|
||||
return delegate.getIdentifier( entity, mergeContext );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
|
||||
delegate.setIdentifier( entity, id, session );
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.hibernate.engine.FetchStyle;
|
|||
import org.hibernate.engine.FetchTiming;
|
||||
import org.hibernate.engine.spi.IdentifierValue;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.event.spi.MergeContext;
|
||||
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
|
||||
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
|
||||
import org.hibernate.metamodel.mapping.internal.IdClassEmbeddable;
|
||||
|
@ -79,6 +80,12 @@ public class AnonymousTupleNonAggregatedEntityIdentifierMapping extends Anonymou
|
|||
return delegate.getIdentifier( entity );
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getIdentifier(Object entity, MergeContext mergeContext) {
|
||||
return delegate.getIdentifier( entity, mergeContext );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIdentifier(Object entity, Object id, SharedSessionContractImplementor session) {
|
||||
delegate.setIdentifier( entity, id, session );
|
||||
|
|
|
@ -149,7 +149,7 @@ public class AnyType extends AbstractType implements CompositeType, AssociationT
|
|||
final EntityPersister concretePersister = guessEntityPersister( entity, factory );
|
||||
return concretePersister == null
|
||||
? null
|
||||
: concretePersister.getIdentifier( entity, null );
|
||||
: concretePersister.getIdentifier( entity, (SharedSessionContractImplementor) null );
|
||||
}
|
||||
|
||||
private EntityPersister guessEntityPersister(Object object, SessionFactoryImplementor factory) {
|
||||
|
|
|
@ -284,7 +284,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
|
|||
final EntityPersister concretePersister = getAssociatedEntityPersister( factory );
|
||||
return concretePersister == null
|
||||
? null
|
||||
: concretePersister.getIdentifier( entity, null );
|
||||
: concretePersister.getIdentifier( entity, (SharedSessionContractImplementor) null );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -353,7 +353,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
|
|||
else {
|
||||
final Class<?> mappedClass = persister.getMappedClass();
|
||||
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
|
||||
id = persister.getIdentifier( x, null );
|
||||
id = persister.getIdentifier( x, (SharedSessionContractImplementor) null );
|
||||
}
|
||||
else {
|
||||
id = x;
|
||||
|
@ -387,7 +387,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
|
|||
}
|
||||
else {
|
||||
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
|
||||
xid = persister.getIdentifier( x, null );
|
||||
xid = persister.getIdentifier( x, (SharedSessionContractImplementor) null );
|
||||
}
|
||||
else {
|
||||
//JPA 2 case where @IdClass contains the id and not the associated entity
|
||||
|
@ -402,7 +402,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
|
|||
}
|
||||
else {
|
||||
if ( mappedClass.isAssignableFrom( y.getClass() ) ) {
|
||||
yid = persister.getIdentifier( y, null );
|
||||
yid = persister.getIdentifier( y, (SharedSessionContractImplementor) null );
|
||||
}
|
||||
else {
|
||||
//JPA 2 case where @IdClass contains the id and not the associated entity
|
||||
|
@ -564,7 +564,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
|
|||
id = lazyInitializer.getInternalIdentifier();
|
||||
}
|
||||
else {
|
||||
id = persister.getIdentifier( value, null );
|
||||
id = persister.getIdentifier( value, (SharedSessionContractImplementor) null );
|
||||
}
|
||||
|
||||
result.append( '#' )
|
||||
|
|
|
@ -1,16 +1,12 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* 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
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
* Copyright Red Hat Inc. and Hibernate Authors
|
||||
*/
|
||||
package org.hibernate.engine.internal;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
|
@ -183,7 +179,7 @@ public class StatisticalLoggingSessionEventListenerTest {
|
|||
// Number of lines
|
||||
assertThat( sessionMetricsLog.lines().count() )
|
||||
.as( "The StatisticalLoggingSessionEventListener should write a line per metric ("
|
||||
+ numberOfMetrics + " lines) plus a header and a footer (2 lines)" )
|
||||
+ numberOfMetrics + " lines) plus a header and a footer (2 lines)" )
|
||||
.isEqualTo( numberOfMetrics + 2 );
|
||||
// Total time
|
||||
long sumDuration = metricList.stream().map( SessionMetric::getDuration ).mapToLong( Long::longValue ).sum();
|
||||
|
|
Loading…
Reference in New Issue