休眠缓存不适用于一对一关系的反面

时间:2019-05-21 07:57:27

标签: hibernate hibernate-mapping hibernate-cache

我尝试使用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
}

我在日志中看到休眠状态没有使用缓存来检索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=?

我通过实验发现,最后的实体加载实际上是查询关系反面的查询。

0 个答案:

没有答案