标签:factor comm persist 类对象 缓存 tran 调用 更新 HERE
@Test
public void test1() {
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=?
*/
s.setSage(999); // ORM思想:操作Managed状态的实体类对象就相当于操作表中的记录
tx.commit();
/*
Hibernate:
update
Student
set
sage=?,
sname=?
where
sid=?
*/
em.close();
factory.close();
}
@Test
public void test2() {
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=?
*/
em.remove(s); // ORM思想:操作Managed状态的实体类对象就相当于操作表中的记录
tx.commit();
/*
Hibernate:
delete
from
Student
where
sid=?
*/
em.close();
factory.close();
}
@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=?
*/
em.remove(s);
em.persist(s);
tx.commit();
em.close();
factory.close();
}
@Test
public void test2() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa");
EntityManager em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Student s = em.find(Student.class, 1027500L); // 找不到返回 null
/*
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); // null
tx.commit();
em.close();
factory.close();
}
标签:factor comm persist 类对象 缓存 tran 调用 更新 HERE
原文地址:https://www.cnblogs.com/wumingoo1/p/13398945.html