我使用的是Eclipselink JPA(2.0)。我需要访问entityManagerFactory来清除所有缓存,因为正常的属性设置或刷新或设置提示对我不起作用。我从这段代码中找到了一种方法:
entityManager.getEntityManagerFactory().getCache().evictAll();
但是在eclipselink entityManager中没有这样的方法getEntityManagerFactory()。我该怎么做?
我对EntityManagerFactory使用Springbean配置,entityManager在DAO层中用@PersistenceContext进行了注释。
在Spring beans配置中:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaDialect" ref="jpaDialect" />
<property name="persistenceUnitName" value="MyService" />
</bean>
在DAO:中
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
谢谢。
如果使用JPA1,则不能使用标准的JPA API,因为当时还不存在该方法。如果使用JPA2,则EM.getEntityManagerFactory()是自添加以来的方式。http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#getEntityManagerFactory()