了解hibernate缓存

时间:2010-07-06 15:00:16

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

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

@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

1 个答案:

答案 0 :(得分:0)

  

(...)我是否还需要将@cache放在getObjectEntries上,如下所示:

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

  

如果我专门添加hibernate.cache.use_query_cache = true

,是否需要为每个查询定义缓存

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

  

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

所以,是的,你仍然需要设置查询 cachable (通过在查询或条件对象上调用setCacheable(true)),如果你想 - 这对IMO来说是件好事。 / p>