Skip to content

Commit 0f35a7d

Browse files
committed
HHH-18702 Exception using @EmbeddedId with @onetomany that refers to an alternate key column
1 parent 9894ed9 commit 0f35a7d

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
@@ -349,7 +349,7 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
349349
}
350350
else {
351351
final Class<?> mappedClass = persister.getMappedClass();
352-
if ( mappedClass.isAssignableFrom( x.getClass() ) ) {
352+
if ( mappedClass.isInstance( x ) ) {
353353
id = persister.getIdentifier( x );
354354
}
355355
else {
@@ -360,8 +360,15 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
360360
}
361361
else {
362362
assert uniqueKeyPropertyName != null;
363+
final Object uniqueKey;
363364
final Type keyType = persister.getPropertyType( uniqueKeyPropertyName );
364-
return keyType.getHashCode( x, factory );
365+
if ( keyType.getReturnedClass().isInstance( x ) ) {
366+
uniqueKey = x;
367+
}
368+
else {
369+
uniqueKey = persister.getPropertyValue( x, uniqueKeyPropertyName );
370+
}
371+
return keyType.getHashCode( uniqueKey, factory );
365372
}
366373
}
367374

@@ -376,39 +383,62 @@ public boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) {
376383
}
377384

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

395-
Object yid;
396-
final LazyInitializer lazyInitializerY = extractLazyInitializer( y );
397-
if ( lazyInitializerY != null ) {
398-
yid = lazyInitializerY.getInternalIdentifier();
418+
// Check for reference equality first as the type-specific checks by IdentifierType are sometimes non-trivial
419+
return (xid == yid) || persister.getIdentifierType().isEqual( xid, yid, factory );
399420
}
400421
else {
401-
if ( mappedClass.isAssignableFrom( y.getClass() ) ) {
402-
yid = persister.getIdentifier( y );
422+
assert uniqueKeyPropertyName != null;
423+
final Object xUniqueKey;
424+
final Type keyType = persister.getPropertyType( uniqueKeyPropertyName );
425+
if ( keyType.getReturnedClass().isInstance( x ) ) {
426+
xUniqueKey = x;
403427
}
404428
else {
405-
//JPA 2 case where @IdClass contains the id and not the associated entity
406-
yid = y;
429+
xUniqueKey = persister.getPropertyValue( x, uniqueKeyPropertyName );
407430
}
408-
}
409431

410-
// Check for reference equality first as the type-specific checks by IdentifierType are sometimes non-trivial
411-
return ( xid == yid ) || persister.getIdentifierType().isEqual( xid, yid, factory );
432+
final Object yUniqueKey;
433+
if ( keyType.getReturnedClass().isInstance( y ) ) {
434+
yUniqueKey = y;
435+
}
436+
else {
437+
yUniqueKey = persister.getPropertyValue( y, uniqueKeyPropertyName );
438+
}
439+
return (xUniqueKey == yUniqueKey)
440+
|| keyType.isEqual( xUniqueKey, yUniqueKey, factory );
441+
}
412442
}
413443

414444
/**

0 commit comments

Comments
 (0)