EntityManager的detach()方法用于从第一级缓存中清除一个特定的对象。对于第二级缓存,JPA API提供了javax.persistence.Cache接口,它具有控制第二级缓存的evict()方法,能从第二级缓存的实体数据缓存中删除特定的数据。
javax.persistence.Cache接口只能控制实体数据缓存。如果要控制其他类型的缓存,需要使用Hibernate API提供的org.hibernate.Cache接口。
以下controlCache()方法演示了JPA API以及Hibernate API的Cache接口的用法:
public void controlCache(){
//Cache接口来自于JPA API的javax.persistence包
Cache cache = entityManagerFactory.getCache();