@@ -349,7 +349,7 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
349
349
}
350
350
else {
351
351
final Class <?> mappedClass = persister .getMappedClass ();
352
- if ( mappedClass .isAssignableFrom ( x . getClass () ) ) {
352
+ if ( mappedClass .isInstance ( x ) ) {
353
353
id = persister .getIdentifier ( x );
354
354
}
355
355
else {
@@ -360,8 +360,15 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
360
360
}
361
361
else {
362
362
assert uniqueKeyPropertyName != null ;
363
+ final Object uniqueKey ;
363
364
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 );
365
372
}
366
373
}
367
374
@@ -376,39 +383,62 @@ public boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) {
376
383
}
377
384
378
385
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 ();
388
392
}
389
393
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
+ }
392
416
}
393
- }
394
417
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 );
399
420
}
400
421
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 ;
403
427
}
404
428
else {
405
- //JPA 2 case where @IdClass contains the id and not the associated entity
406
- yid = y ;
429
+ xUniqueKey = persister .getPropertyValue ( x , uniqueKeyPropertyName );
407
430
}
408
- }
409
431
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
+ }
412
442
}
413
443
414
444
/**
0 commit comments