JPA Hibernate EHCACHE issue with onetoone associations - QueryCache
I have an entity directly mapped to UI which has some lookup tables where the data would not change at all. In my Controller I am using findAll on the look up tables to get all the values and set it to the model.
Main entity
@Entity
public class MainEntity implements Serializable {
@OneToOne(cascade = CascadeType.ALL, optional=true)
@JoinColumn(name="lookup_e开发者_如何学Pythonntity_key")
private LookupEntity luentity;
}
Lookup entity:
@Entity
@Table(name="lookup_entity")
@Cacheable
public class LookupEntity implements Serializable {
}
I have enabled the second level cache in persistence.xml. configuration below
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />
<property name="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" />
FindALL Query Cache:
entityManager.createQuery("from " + this.entityClass.getName() )
.setHint("org.hibernate.cacheable", true)
.getResultList();
When ever the request is coming from the UI it tries to call the findAll methods in LookupEntity object. For some reason the first request calls the findAll sql correctly but the susequent request are invoking a findById SQL for all the rows present in the LookupEntity. Is this a behaviour that is expected or is something wrong with my configuration.
Please help!!!
Thanks!
It is expected behaviour : the query cache will return the IDs of the entity, and Hibernate will the load the entities themselves. Entities which are queried by a cached query should thus be in the second level entity cache in order for the operation to be beneficial.
This is in the documentation :
Note
The query cache does not cache the state of the actual entities in the cache; it caches only identifier values and results of value type. For this reaso, the query cache should always be used in conjunction with the second-level cache for those entities expected to be cached as part of a query result cache (just as with collection caching).
精彩评论