问题
I tried googling this and found a possible duplicate of the question, but I cannot preview it: https://stackoverflow.com/questions/49344593/inverse-side-one-to-one-relationship-caching-not-working
Given two domain model classess:
@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
}
and
@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
}
I can see in the log that hibernate does not utilize caching for retrieving ObjectB#objectA
field.
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=?
I found out through experimentation that this last entity load is in fact query for loading inverse side of relationship.
来源:https://stackoverflow.com/questions/56233710/hibernate-caching-not-working-for-inverse-side-of-one-to-one-relationship