【问题标题】:understanding hibernate cache了解休眠缓存
【发布时间】:2011-03-12 08:55:33
【问题描述】:

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

@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

?

【问题讨论】:

    标签: java hibernate orm second-level-cache query-cache


    【解决方案1】:

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

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

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

    来自关于hibernate.cache.use_query_cache 属性的参考文档(3.4. Optional configuration properties 部分):

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

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

    【讨论】:

    • 还有一个问题。为什么我需要缓存 getObjectEntries 的集合?不会太大内存吗? Hibernate 可以只保存对存在的对象的引用而不是对所有集合的引用吗?
    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2016-08-13
    • 2018-01-09
    • 2012-04-29
    • 2015-01-05
    • 2011-11-18
    • 2017-01-16
    相关资源
    最近更新 更多