2017-08-16 1 views
1

J'utilise Java 7 avec Hibernate 5.1.5.Final. J'essaie de comprendre comment mettre en cache une requête qui se produit à la suite du chargement d'une collection paresseuse. Voici mon entité ...Comment configurer une collection paresseuse pour utiliser mon cache de second niveau Hibernate?

@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; 
    } 

Mon cache de second niveau est configuré (par Spring) comme si ...

<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> 

Bien que l'entité elle-même est marquée par l'annotation "@Cache", appeler "getChildren" aboutit à une requête. Comment configurer mon entité (ou application) pour que je profite du cache?

Répondre

0

Constaté que l'annotation @Cache était la façon de le faire ...

@OneToMany(fetch = FetchType.LAZY) 
@JoinColumn(name = "PARENT_ID") 
@OrderBy("orderNumber") 
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL) 
private Set<MyItem> children = new TreeSet<MyItem>();