如何删除Spring Hibernate EntityManager中的Cache
1、不知道为什么得到对象后,再执行Procedure,再重新提取这个对象时,就怎么都得不到Procedure中更新的数据
@Autowired PersonRepository personRepository;
@Autowired PersonDao personDao;
@Autowired EntityManager entityManager;
Person result = personRepository.save(person);
personDao.updateName(result.getId()); //调用了数据库的Procedure,更新了Person的数据,但之后我怎么都得不到这个更新的数据,还是旧的数据
//这里用了所有我所知道的删除Cache的方法,一个一个试,没有一个好使的,不知道为什么
entityManager.clear();
EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
entityManagerFactory.getCache().evictAll();
SessionFactory sessionFactory = ((HibernateEntityManagerFactory)entityManagerFactory).getSessionFactory();
Cache cache = sessionFactory.getCache();
cache.evictEntityRegions();
cache.evictCollectionRegions();
cache.evictDefaultQueryRegion();
cache.evictDefaultQueryRegion();
Session session = entityManager.unwrap(Session.class);
session.clear();
sessionFactory = session.getSessionFactory();
cache = sessionFactory.getCache();
cache.evictCollectionRegions();
cache.evictEntityRegions();
result = topoRepository.findOne(result.getId()); //这里得到的还是调用Procedure之前数据
2、不知道为什么,在@Test里就好使,能得到调用Procedure更新后的数据,用的是同样的方法
@Autowired PersonRepository personRepository;
@Autowired PersonDao personDao;
@Test
public void testUpdateRefId(){
Person result = personRepository.save(person);
personDao.updateName(result.getId());
result = topoRepository.findOne(result.getId());
}
3、其它信息:
主程序用的是Spring Boot
@SpringBootApplication
public class Application {
请大家指教。