Skip to content

Commit 145fe6c

Browse files
committed
HHH-18702 Exception using @EmbeddedId with @onetomany that refers to an alternate key column
1 parent a9290b2 commit 145fe6c

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

hibernate-core/src/main/java/org/hibernate/type/EntityType.java

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
352352
}
353353
else {
354354
final Class<?> mappedClass = persister.getMappedClass();
355-
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
355+
if ( mappedClass.isInstance( x ) ) {
356356
id = persister.getIdentifier( x, (SharedSessionContractImplementor) null );
357357
}
358358
else {
@@ -363,8 +363,15 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
363363
}
364364
else {
365365
assert uniqueKeyPropertyName != null;
366+
final Object uniqueKey;
366367
final Type keyType = persister.getPropertyType( uniqueKeyPropertyName );
367-
return keyType.getHashCode( x, factory );
368+
if ( keyType.getReturnedClass().isInstance( x ) ) {
369+
uniqueKey = x;
370+
}
371+
else {
372+
uniqueKey = persister.getPropertyValue( x, uniqueKeyPropertyName );
373+
}
374+
return keyType.getHashCode( uniqueKey, factory );
368375
}
369376
}
370377

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

381388
final EntityPersister persister = getAssociatedEntityPersister( factory );
382-
final Class<?> mappedClass = persister.getMappedClass();
383-
Object xid;
384-
final LazyInitializer lazyInitializerX = extractLazyInitializer( x );
385-
if ( lazyInitializerX != null ) {
386-
xid = lazyInitializerX.getInternalIdentifier();
387-
}
388-
else {
389-
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
390-
xid = persister.getIdentifier( x, (SharedSessionContractImplementor) null );
389+
if ( isReferenceToPrimaryKey() ) {
390+
final Class<?> mappedClass = persister.getMappedClass();
391+
Object xid;
392+
final LazyInitializer lazyInitializerX = extractLazyInitializer( x );
393+
if ( lazyInitializerX != null ) {
394+
xid = lazyInitializerX.getInternalIdentifier();
391395
}
392396
else {
393-
//JPA 2 case where @IdClass contains the id and not the associated entity
394-
xid = x;
397+
if ( mappedClass.isInstance( x ) ) {
398+
xid = persister.getIdentifier( x, (SharedSessionContractImplementor) null );
399+
}
400+
else {
401+
//JPA 2 case where @IdClass contains the id and not the associated entity
402+
xid = x;
403+
}
404+
}
405+
406+
Object yid;
407+
final LazyInitializer lazyInitializerY = extractLazyInitializer( y );
408+
if ( lazyInitializerY != null ) {
409+
yid = lazyInitializerY.getInternalIdentifier();
410+
}
411+
else {
412+
if ( mappedClass.isInstance( y ) ) {
413+
yid = persister.getIdentifier( y, (SharedSessionContractImplementor) null );
414+
}
415+
else {
416+
//JPA 2 case where @IdClass contains the id and not the associated entity
417+
yid = y;
418+
}
395419
}
396-
}
397420

398-
Object yid;
399-
final LazyInitializer lazyInitializerY = extractLazyInitializer( y );
400-
if ( lazyInitializerY != null ) {
401-
yid = lazyInitializerY.getInternalIdentifier();
421+
// Check for reference equality first as the type-specific checks by IdentifierType are sometimes non-trivial
422+
return ( xid == yid ) || persister.getIdentifierType().isEqual( xid, yid, factory );
402423
}
403424
else {
404-
if ( mappedClass.isAssignableFrom( y.getClass() ) ) {
405-
yid = persister.getIdentifier( y, (SharedSessionContractImplementor) null );
425+
assert uniqueKeyPropertyName != null;
426+
final Object xUniqueKey;
427+
final Type keyType = persister.getPropertyType( uniqueKeyPropertyName );
428+
if ( keyType.getReturnedClass().isInstance( x ) ) {
429+
xUniqueKey = x;
406430
}
407431
else {
408-
//JPA 2 case where @IdClass contains the id and not the associated entity
409-
yid = y;
432+
xUniqueKey = persister.getPropertyValue( x, uniqueKeyPropertyName );
410433
}
411-
}
412434

413-
// Check for reference equality first as the type-specific checks by IdentifierType are sometimes non-trivial
414-
return ( xid == yid ) || persister.getIdentifierType().isEqual( xid, yid, factory );
435+
final Object yUniqueKey;
436+
if ( keyType.getReturnedClass().isInstance( y ) ) {
437+
yUniqueKey = y;
438+
}
439+
else {
440+
yUniqueKey = persister.getPropertyValue( y, uniqueKeyPropertyName );
441+
}
442+
return (xUniqueKey == yUniqueKey)
443+
|| keyType.isEqual( xUniqueKey, yUniqueKey, factory );
444+
}
415445
}
416446

417447
/**

0 commit comments

Comments
 (0)