JPA EntiityManager.refresh方法
Posted wumingoo1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA EntiityManager.refresh方法相关的知识,希望对你有一定的参考价值。
refresh 方法
- 刷新:将数据库中的数据刷新到Managed状态的实体中
refresh 方法测试
find 之后 setter 之后 refresh
@Test
public void test() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Student s = em.find(Student.class, 1L);
/*
Hibernate:
select
student0_.sid as sid1_0_0_,
student0_.sage as sage2_0_0_,
student0_.sname as sname3_0_0_
from
Student student0_
where
student0_.sid=?
*/
System.out.println(s); // Student{sid=1, sname=‘刘备‘, sage=23}
s.setSname("陈宫");
System.out.println(s); // Student{sid=1, sname=‘陈宫‘, sage=23}
em.refresh(s);
/*
Hibernate:
select
student0_.sid as sid1_0_0_,
student0_.sage as sage2_0_0_,
student0_.sname as sname3_0_0_
from
Student student0_
where
student0_.sid=?
*/
System.out.println(s); // Student{sid=1, sname=‘刘备‘, sage=23}
tx.commit();
em.close();
factory.close();
}
以上是关于JPA EntiityManager.refresh方法的主要内容,如果未能解决你的问题,请参考以下文章