如何从Eclipselink中的EntityManager访问EntityManagerFactory

时间:2012-01-20 04:29:30

标签: java caching jpa eclipselink entitymanager

我正在使用Eclipselink JPA(2.0)。我需要访问entityManagerFactory来清除所有缓存,因为正常的属性设置或刷新或设置提示对我不起作用。我从这段代码中找到了一种方法:

entityManager.getEntityManagerFactory().getCache().evictAll(); 

但是在eclipselink entityManager中没有名为getEntityManagerFactory()的方法。我该怎么做?

我正在为EntityManagerFactory使用Spring bean配置,并且在DAO层中使用@PersistenceContext注释entityManager。

在Spring beans config中:

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

感谢。

1 个答案:

答案 0 :(得分:1)

如果使用JPA1,则无法使用标准JPA API,因为该方法不存在。 如果使用JPA2,则EM.getEntityManagerFactory()是自那里添加以来的方式。 http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#getEntityManagerFactory()

相关问题