了解hibernate缓存

发布于 2024-09-08 08:25:36 字数 1107 浏览 1 评论 0原文

如果我在对象类中有这个方法:

@OneToMany( fetch = FetchType.EAGER,
    cascade = { CascadeType.ALL },
    mappedBy = "object"  )
@org.hibernate.annotations.Cascade(
    {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Column( nullable = false  )
public Set<ObjectEntry> getObjectEntries() {
    return this.objectEntries;
}

并且我将 @cache 都放在 ObjectEntryObject

@Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
public class Object extends HashCodeValidator {

@Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
public class ObjectEntry extends HashCodeValidator 

,我还需要将 @cache 放在getObjectEntries 像这样:

@org.hibernate.annotations.Cascade(
    {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Column( nullable = false  )
@Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
public Set<ObjectEntry> getObjectEntries() {
    return this.objectEntries;
}

如果我专门添加,是否需要为每个查询定义缓存

hibernate.cache.use_query_cache = true

If I have this method in object class:

@OneToMany( fetch = FetchType.EAGER,
    cascade = { CascadeType.ALL },
    mappedBy = "object"  )
@org.hibernate.annotations.Cascade(
    {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Column( nullable = false  )
public Set<ObjectEntry> getObjectEntries() {
    return this.objectEntries;
}

and I put @cache both on ObjectEntry and on Object

@Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
public class Object extends HashCodeValidator {

@Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
public class ObjectEntry extends HashCodeValidator 

Do I still need to put @cache on getObjectEntries like this:

@org.hibernate.annotations.Cascade(
    {org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Column( nullable = false  )
@Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
public Set<ObjectEntry> getObjectEntries() {
    return this.objectEntries;
}

Do I need to define cache for each query if I specifically add

hibernate.cache.use_query_cache = true

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-09-15 08:25:36

(...)我还需要像这样将 @cache 放在 getObjectEntries 上吗:

是的,如果您愿意,您仍然必须缓存集合(它将缓存在特定的缓存区域中)。

如果我专门添加 hibernate.cache.use_query_cache = true,是否需要为每个查询定义缓存

如果我专门从有关 hibernate.cache.use_query_cache 属性的参考文档中 (第 3.4. 可选配置属性):

启用查询缓存。单个查询仍然必须设置为可缓存。 例如 true|false

所以,是的,您仍然需要设置一个查询可缓存(通过调用 setCacheable(true) 在查询或条件对象上)如果你愿意的话 - 在我看来这是一件好事。

(...) Do I still need to put @cache on getObjectEntries like this:

Yes, you still have to cache a collection if you want to (that will be cached in a specific cache region).

Do I need to define cache for each query if I specifically add hibernate.cache.use_query_cache = true

From the reference documentation about the hibernate.cache.use_query_cache property (section 3.4. Optional configuration properties):

Enables the query cache. Individual queries still have to be set cachable. e.g. true|false

So, yes, you still have to set a query cachable (by calling setCacheable(true) on a query or criteria object) if you want to - which is IMO a good thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文