Java 选择具有复合id的对象时休眠查询缓存
我很难弄清楚如何在以下实体上有效地使用查询缓存条件查询:Java 选择具有复合id的对象时休眠查询缓存,java,hibernate,caching,criteria,ehcache,Java,Hibernate,Caching,Criteria,Ehcache,我很难弄清楚如何在以下实体上有效地使用查询缓存条件查询: @Entity @Table(name = "category_configuration_values") @Immutable @Cacheable @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public class CategoryConfigurationValue implements Serializable {
@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();
如果我下次再执行它,我会得到很多以下信息,对我来说,这些信息看起来像是缓存未命中:
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=?
...
这里可能缺少什么?如果启用了查询缓存,则需要显式地将类
CategoryConfiguration Value
添加为可缓存,然后将该类的所有实例标记为可缓存,这将解决您的问题
- 阿南莎·夏尔玛
CategoryConfigurationValue
添加为可缓存,然后将该类的所有实例标记为可缓存,这将解决您的问题
- 阿南莎·夏尔玛
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=?
...