gpt4 book ai didi

java - Hibernate 二级缓存一对一不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:56 25 4
gpt4 key购买 nike

我将 Ehcache 提供程序用于 Hibernate 二级缓存。它缓存一对多集合,用@Cache注解,但不缓存一对一:

//hb annotations
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "details")
private Details details;

//getters, setters, constructors etc.

}

//hb annotations
public class Details {

@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "user"))
@Id
@GeneratedValue(generator = "generator")
@Column(unique = true, nullable = false)
private Integer id;

@OneToOne
@PrimaryKeyJoinColumn
private User user;

//getters, setters, constructors ets.

}

我使用 Spring JpaRepository 来获取数据:

userRepository.findOne(id);

最佳答案

对于 ToOne 关联 only the id (foreign key)关联实体实例的缓存在拥有实例的条目中:

Hibernate stores cached entities in a dehydrated form, which is similar to the database representation. Aside from the foreign key column values of the @ManyToOne or @OneToOne child-side associations, entity relationships are not stored in the cache.

因此,当拥有实例从 L2 缓存中组装时,关联实体必须通过其 id 加载。为避免这种情况,请使关联实体(在您的情况下为 Details)也可缓存。

还有更多详细信息 herehere .

关于java - Hibernate 二级缓存一对一不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40033386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com