以下em.refresh(person)
代码中的不起作用。它不会使用数据库中的新值刷新person
,而是重置(撤消或丢弃)缓存中所做的更改。我无法理解为什么?
em.getTransaction().begin();
Person person = em.find(Person.class, 2L); //Person[id=2L, age=23]
person.setAge(24);
System.out.println(person.getAge()); //it prints 24
//Person with id=2 in database gets modified concurrently somehow,
//its age becomes 25 in PERSON table (by an SQL update for example "UPDATE person SET age=25 WHERE id=2")
em.refresh(person); // attempts to load fresh value from database with a SELECT...
System.out.println(person.getAge()); //it prints 23, rather than 25, why?
em.getTransaction().commit();
em.close();
refresh()
有人可以通过以下方法帮助我理解这种行为EntityManager
吗?