| Given the following entities: {{ @Entity @Table(name = "person") @Cacheable static class Person { @Id @GeneratedValue private long id; @OneToOne @Fetch(FetchMode.SELECT) private Passport passport; } @Entity @Table(name = "passport") @Cacheable static class Passport { @Id @GeneratedValue private long id; @OneToOne(mappedBy = "passport") @Fetch(FetchMode.SELECT) private Person person; } }} When a Person is loaded from the database or second-level cache, Person#passport is eagerly loaded. Currently, when the Passport entity is loaded, Hibernate loads Passport#person by unique ID from the database, even though the associated Person is already in the persistence context. The problem is that, currently, Person is only accessible from the persistence context by ID, not by unique ID. If Hibernate adds Person to the persistence context mapped by the unique key (via PersistenceContext#addEntity(EntityUniqueKey euk, Object entity)) when it is being hydrated or assembled, then loading Person by the unique ID from the database can be avoided. Instead, EntityType#loadByUniqueKey will successfully lookup Person by unique key via PersistenceContext#getEntity(EntityUniqueKey euk). |