我正在使用Java 7和Hibernate 5.1.5.Final。我试图弄清楚如何缓存由于加载延迟集合而发生的查询。这是我的实体......
@Entity
@Table(name = "my_item")
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MyItem implements Serializable, Comparable<MyItem>
...
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID")
@OrderBy("orderNumber")
private Set<MyItem> children = new TreeSet<MyItem>();
...
public Set<MyItem> getChildren() {
return children;
}
我的二级缓存配置(通过Spring)就像这样......
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="org.collegeboard.springboard" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="jpaPropertyMap" ref="jpaPropertyMap" />
</bean>
<util:map id="jpaPropertyMap">
<entry key="hibernate.show_sql" value="false" />
<entry key="hibernate.hbm2ddl.auto" value="validate"/>
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<entry key="hibernate.cache.use_second_level_cache" value="true" />
<entry key="hibernate.cache.use_query_cache" value="${enable.hibernate.query.cache}" />
<entry key="hibernate.generate_statistics" value="false" />
<entry key="hibernate.event.merge.entity_copy_observer" value="allow" />
<entry key="hibernate.enable_lazy_load_no_trans" value="true" />
</util:map>
虽然实体本身标有“@Cache”注释,但调用“getChildren”会导致查询。如何配置我的实体(或应用程序)以便利用缓存?
答案 0 :(得分:0)
发现@Cache注释是实现此目的的方法......
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID")
@OrderBy("orderNumber")
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
private Set<MyItem> children = new TreeSet<MyItem>();