HHH-18390 Fix hash code for entity types not referring to primary keys

This commit is contained in:
Marco Belladelli 2024-07-23 10:06:58 +02:00
parent baafcd9fbb
commit 1aa7db6a0a
1 changed files with 19 additions and 13 deletions

View File

@ -281,8 +281,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
} }
private Object extractIdentifier(Object entity, SessionFactoryImplementor factory) { private Object extractIdentifier(Object entity, SessionFactoryImplementor factory) {
final EntityPersister concretePersister = final EntityPersister concretePersister = getAssociatedEntityPersister( factory );
factory.getMappingMetamodel().getEntityDescriptor( associatedEntityName );
return concretePersister == null return concretePersister == null
? null ? null
: concretePersister.getIdentifier( entity, null ); : concretePersister.getIdentifier( entity, null );
@ -345,21 +344,28 @@ public abstract class EntityType extends AbstractType implements AssociationType
@Override @Override
public int getHashCode(Object x, SessionFactoryImplementor factory) { public int getHashCode(Object x, SessionFactoryImplementor factory) {
final EntityPersister persister = getAssociatedEntityPersister( factory ); final EntityPersister persister = getAssociatedEntityPersister( factory );
final Object id; if ( isReferenceToPrimaryKey() ) {
final LazyInitializer lazyInitializer = extractLazyInitializer( x ); final Object id;
if ( lazyInitializer != null ) { final LazyInitializer lazyInitializer = extractLazyInitializer( x );
id = lazyInitializer.getInternalIdentifier(); if ( lazyInitializer != null ) {
} id = lazyInitializer.getInternalIdentifier();
else {
final Class<?> mappedClass = persister.getMappedClass();
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
id = persister.getIdentifier( x, null );
} }
else { else {
id = x; final Class<?> mappedClass = persister.getMappedClass();
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
id = persister.getIdentifier( x, null );
}
else {
id = x;
}
} }
return persister.getIdentifierType().getHashCode( id, factory );
}
else {
assert uniqueKeyPropertyName != null;
final Type keyType = persister.getPropertyType( uniqueKeyPropertyName );
return keyType.getHashCode( x, factory );
} }
return persister.getIdentifierType().getHashCode( id, factory );
} }
@Override @Override