选择具有复合 id 的对象时 Hibernate 查询缓存

发布于 2024-09-30 16:13:30 字数 3424 浏览 0 评论 0原文

我很难弄清楚如何在以下实体上有效地使用查询缓存条件查询:

@Entity @Table(name = "category_configuration_values")
@Immutable
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class CategoryConfigurationValue implements Serializable {
    private static final long serialVersionUID = 3L;
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryConfigurationValue.class);

    @EmbeddedId
    private CategoryConfigurationValuePk primaryKey;

    @Column(name = "value")
    private String value;

    @Override
    public boolean equals(Object o) { ... }

    @Override
    public int hashCode() { ... }
}

@Embeddable
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
class CategoryConfigurationValuePk implements Serializable {
    private static final long serialVersionUID = 5068893389269876464L;

    @Column(name = "configuration_type_id")
    private int configurationTypeId;

    @Column(name = "category_id", columnDefinition = "smallint")
    private int categoryId;

    @Override
    public int hashCode() { ... }

    @Override
    public boolean equals(Object obj) { ... }

}

导致缓存未命中的条件之一是:

    Criteria criteria = getCurrentSession().createCriteria(CategoryConfigurationValue.class);
    criteria.setCacheable(true);
    criteria.setCacheRegion("query.AllConfigurationValuesForCategoriesAndAncestors");
    criteria.add(Restrictions.in("primaryKey.categoryId", categoryIds));

    List<CategoryConfigurationValue> allCategoryConfigurationValues = criteria.list();

第一次执行时我得到“in”查询:

Hibernate: select this_.category_id as category1_4_0_, this_.configuration_type_id as configur2_4_0_, this_.value as value4_0_ from category_configuration_values this_ where this_.category_id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

如果我再次执行它,我会得到很多以下信息,对我来说这看起来像是缓存未命中:

    Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
...

这里我可能会丢失什么?

I'm having a hard time figuring out how to make effective use of query caching criteria queries on the following entity:

@Entity @Table(name = "category_configuration_values")
@Immutable
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class CategoryConfigurationValue implements Serializable {
    private static final long serialVersionUID = 3L;
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryConfigurationValue.class);

    @EmbeddedId
    private CategoryConfigurationValuePk primaryKey;

    @Column(name = "value")
    private String value;

    @Override
    public boolean equals(Object o) { ... }

    @Override
    public int hashCode() { ... }
}

@Embeddable
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
class CategoryConfigurationValuePk implements Serializable {
    private static final long serialVersionUID = 5068893389269876464L;

    @Column(name = "configuration_type_id")
    private int configurationTypeId;

    @Column(name = "category_id", columnDefinition = "smallint")
    private int categoryId;

    @Override
    public int hashCode() { ... }

    @Override
    public boolean equals(Object obj) { ... }

}

The one of the criteria which is resulting in cache misses is:

    Criteria criteria = getCurrentSession().createCriteria(CategoryConfigurationValue.class);
    criteria.setCacheable(true);
    criteria.setCacheRegion("query.AllConfigurationValuesForCategoriesAndAncestors");
    criteria.add(Restrictions.in("primaryKey.categoryId", categoryIds));

    List<CategoryConfigurationValue> allCategoryConfigurationValues = criteria.list();

The first time is is executed I get the 'in' query:

Hibernate: select this_.category_id as category1_4_0_, this_.configuration_type_id as configur2_4_0_, this_.value as value4_0_ from category_configuration_values this_ where this_.category_id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

If I execute it another time I get a lot of the following, which to me looks like cache misses:

    Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
Hibernate: select categoryco0_.category_id as category1_4_0_, categoryco0_.configuration_type_id as configur2_4_0_, categoryco0_.value as value4_0_ from category_configuration_values categoryco0_ where categoryco0_.category_id=? and categoryco0_.configuration_type_id=?
...

What could I be missing here?

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

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

发布评论

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

评论(1

墨小沫ゞ 2024-10-07 16:13:30

即使您已启用查询缓存,您也需要显式地将类 CategoryConfigurationValue 添加为可缓存,然后该类的所有实例都被标记为可缓存,这将解决您的问题。.

  • Anantha Sharma

eventhough you've enabled query caching, you need to explicitly add the class CategoryConfigurationValue as cachable, then all instances of the class is marked as cachable, this would solve your problem..

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