Skip to content

Commit

Permalink
HHH-18702 Exception using @EmbeddedId with @onetomany that refers to …
Browse files Browse the repository at this point in the history
…an alternate key column
  • Loading branch information
dreab8 committed Oct 21, 2024
1 parent 21f77b6 commit 9b2885f
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions hibernate-core/src/main/java/org/hibernate/type/EntityType.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
}
else {
final Class<?> mappedClass = persister.getMappedClass();
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
if ( mappedClass.isInstance( x ) ) {
id = persister.getIdentifier( x, (SharedSessionContractImplementor) null );
}
else {
Expand All @@ -363,8 +363,15 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
}
else {
assert uniqueKeyPropertyName != null;
final Object uniqueKey;
final Type keyType = persister.getPropertyType( uniqueKeyPropertyName );
return keyType.getHashCode( x, factory );
if ( keyType.getReturnedClass().isInstance( x ) ) {
uniqueKey = x;
}
else {
uniqueKey = persister.getPropertyValue( x, uniqueKeyPropertyName );
}
return keyType.getHashCode( uniqueKey, factory );
}
}

Expand All @@ -379,39 +386,62 @@ public boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) {
}

final EntityPersister persister = getAssociatedEntityPersister( factory );
final Class<?> mappedClass = persister.getMappedClass();
Object xid;
final LazyInitializer lazyInitializerX = extractLazyInitializer( x );
if ( lazyInitializerX != null ) {
xid = lazyInitializerX.getInternalIdentifier();
}
else {
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
xid = persister.getIdentifier( x, (SharedSessionContractImplementor) null );
if ( isReferenceToPrimaryKey() ) {
final Class<?> mappedClass = persister.getMappedClass();
Object xid;
final LazyInitializer lazyInitializerX = extractLazyInitializer( x );
if ( lazyInitializerX != null ) {
xid = lazyInitializerX.getInternalIdentifier();
}
else {
//JPA 2 case where @IdClass contains the id and not the associated entity
xid = x;
if ( mappedClass.isInstance( x ) ) {
xid = persister.getIdentifier( x, (SharedSessionContractImplementor) null );
}
else {
//JPA 2 case where @IdClass contains the id and not the associated entity
xid = x;
}
}

Object yid;
final LazyInitializer lazyInitializerY = extractLazyInitializer( y );
if ( lazyInitializerY != null ) {
yid = lazyInitializerY.getInternalIdentifier();
}
else {
if ( mappedClass.isInstance( y ) ) {
yid = persister.getIdentifier( y, (SharedSessionContractImplementor) null );
}
else {
//JPA 2 case where @IdClass contains the id and not the associated entity
yid = y;
}
}
}

Object yid;
final LazyInitializer lazyInitializerY = extractLazyInitializer( y );
if ( lazyInitializerY != null ) {
yid = lazyInitializerY.getInternalIdentifier();
// Check for reference equality first as the type-specific checks by IdentifierType are sometimes non-trivial
return ( xid == yid ) || persister.getIdentifierType().isEqual( xid, yid, factory );
}
else {
if ( mappedClass.isAssignableFrom( y.getClass() ) ) {
yid = persister.getIdentifier( y, (SharedSessionContractImplementor) null );
assert uniqueKeyPropertyName != null;
final Object xUniqueKey;
final Type keyType = persister.getPropertyType( uniqueKeyPropertyName );
if ( keyType.getReturnedClass().isInstance( x ) ) {
xUniqueKey = x;
}
else {
//JPA 2 case where @IdClass contains the id and not the associated entity
yid = y;
xUniqueKey = persister.getPropertyValue( x, uniqueKeyPropertyName );
}
}

// Check for reference equality first as the type-specific checks by IdentifierType are sometimes non-trivial
return ( xid == yid ) || persister.getIdentifierType().isEqual( xid, yid, factory );
final Object yUniqueKey;
if ( keyType.getReturnedClass().isInstance( y ) ) {
yUniqueKey = y;
}
else {
yUniqueKey = persister.getPropertyValue( y, uniqueKeyPropertyName );
}
return (xUniqueKey == yUniqueKey)
|| keyType.isEqual( xUniqueKey, yUniqueKey, factory );
}
}

/**
Expand Down

0 comments on commit 9b2885f

Please sign in to comment.