JPA SPEC中没有定义关于一级、二级缓存的规范(事实上JCP对各JSR都很少定义涉及性能优化规范),要启用二级缓存需要使用JPA Provider专有的配置,JBoss(Hibernate)中配置如下。
Specify the following code piece in persistence-unit.xml#/persistence-unit/properties/
<property name="hibernate.cache.use_query_cache" value="true" /> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
Add an '@Cache' annatation to the entity class, and choose the right caching strategy.
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)