Я попытался найти это в Google и нашел возможный дубликат вопроса, но не могу его предварительно просмотреть: https://stackoverflow.com/questions/49344593/inverse-side-one-to-one-relationship-caching-not-working
Учитывая две модели доменной модели:
@Entity
@Table(name = "object_a")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ObjectA extends AbstractEntity {
private String name;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "object_b_id")
private ObjectB objectB;
// getters and setters omitted
}
и
@Entity
@Table(name = "object_b")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ObjectB extends AbstractEntity {
private String name;
@OneToOne(mappedBy = "objectB", fetch = FetchType.EAGER)
private ObjectA objectA;
// getters and setters omitted
}
Я вижу в журнале, что hibernate не использует кэширование для извлечения поля ObjectB#objectA
.
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener : Loading entity: [xy.abcde.test12.model.cachetest.ObjectA#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener : Attempting to resolve: [xy.abcde.test12.model.cachetest.ObjectA#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener : Converting second-level cache entry [CacheEntry(xy.abcde.test12.model.cachetest.ObjectA {objA, xy.abcde.test12.model.cachetest.ObjectB#1})] into entity : [xy.abcde.test12.model.cachetest.ObjectA#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener : Loading entity: [xy.abcde.test12.model.cachetest.ObjectB#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener : Attempting to resolve: [xy.abcde.test12.model.cachetest.ObjectB#1]
2019-05-21 09:29:25.049 TRACE 11808 --- [nio-8080-exec-1] o.h.e.internal.DefaultLoadEventListener : Converting second-level cache entry [CacheEntry(xy.abcde.test12.model.cachetest.ObjectB {objB, xy.abcde.test12.model.cachetest.ObjectA#1})] into entity : [xy.abcde.test12.model.cachetest.ObjectB#1]
2019-05-21 09:29:25.049 DEBUG 11808 --- [nio-8080-exec-1] org.hibernate.loader.Loader : Loading entity: [xy.abcde.test12.model.cachetest.ObjectA#1]
2019-05-21 09:29:25.049 DEBUG 11808 --- [nio-8080-exec-1] org.hibernate.SQL : select objecta0_.id as id1_61_1_, objecta0_.name as name2_61_1_, objecta0_.object_b_id as object_b3_61_1_, objectb1_.id as id1_62_0_, objectb1_.name as name2_62_0_ from object_a objecta0_ left outer join object_b objectb1_ on objecta0_.object_b_id=objectb1_.id where objecta0_.object_b_id=?
Я обнаружил в результате экспериментов, что эта последняя загрузка сущности на самом деле является запросом на загрузку.обратная сторона отношений.