操作JPA中的增删改查
//相当于hibernate中session中的get方法
@Test
public void testFind() {
Customer customer = entityManager.find(Customer.class, 1);
System.out.println("--------------------------------------");
System.out.println(customer);
}
//相当于hibernate中session中load方法,存在来加载,只要调用到了对象,才会执行方法
@Test
public void testGetReferece() {
Customer customer = entityManager.getReference(Customer.class, 1);
System.out.println("--------------------------------------");
System.out.println(customer);
}
//类似hibernate中save方法
@Test
public void testPersistence() {
Customer customer=new Customer();
customer.setLastName("刘雯晴1");
customer.setAge(20);
customer.setBirth(new Date());
customer.setCreatetime(new Date());
customer.setEmail("1846882398@qq.com");
entityManager.persist(customer);
System.out.println("插入的ID"+customer.getId());
}
//删除类似于hibernate中session对象中delete方法
//但:注意只能移除移除持久化对象,不能删除游离状态的对象,而hibernate可以删除
@Test
public void testDelete() {
//需要先查询再删除,与hibernate中的delete有区别
//1.以下情况删除会报错,因为对象处于一个游离的状态,只能删除持久化状态的对象
/*Customer customer=new Customer();
customer.setId(1);*/
//2.获取一个持久化状态的对象
Customer customer = entityManager.find(Customer.class, 2);
entityManager.remove(customer);
}
有以下四种情况
//若传入的是一个游离对象, 即传入的对象有 OID.
//1. 若在 EntityManager 缓存中有对应的对象
//2. JPA 会把游离对象的属性复制到查询到EntityManager 缓存中的对象中.
//3. EntityManager 缓存中的对象执行 UPDATE.
@Test
public void testMerge4(){
Customer customer = new Customer();
customer.setAge(18);
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setEmail("dd@163.com");
customer.setLastName("DD");
customer.setId(4);
Customer customer2 = entityManager.find(Customer.class, 4);
entityManager.merge(customer);
System.out.println(customer == customer2); //false
}
//若传入的是一个游离对象, 即传入的对象有 OID.
//1. 若在 EntityManager 缓存中没有该对象
//2. 若在数据库中也有对应的记录
//3. JPA 会查询对应的记录, 然后返回该记录对一个的对象, 再然后会把游离对象的属性复制到查询到的对象中.
//4. 对查询到的对象执行 update 操作.
@Test
public void testMerge3(){
Customer customer = new Customer();
customer.setAge(18);
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setEmail("ee@163.com");
customer.setLastName("EE");
customer.setId(4);
Customer customer2 = entityManager.merge(customer);
System.out.println(customer == customer2); //false
}
//若传入的是一个游离对象, 即传入的对象有 OID.
//1. 若在 EntityManager 缓存中没有该对象
//2. 若在数据库中也没有对应的记录
//3. JPA 会创建一个新的对象, 然后把当前游离对象的属性复制到新创建的对象中
//4. 对新创建的对象执行 insert 操作.
@Test
public void testMerge2(){
Customer customer = new Customer();
customer.setAge(18);
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setEmail("dd@163.com");
customer.setLastName("DD");
customer.setId(100);
Customer customer2 = entityManager.merge(customer);
System.out.println("customer#id:" + customer.getId());
System.out.println("customer2#id:" + customer2.getId());
}
/**
* 总的来说: 类似于 hibernate Session 的 saveOrUpdate 方法.
*/
//1. 若传入的是一个临时对象
//会创建一个新的对象, 把临时对象的属性复制到新的对象中, 然后对新的对象执行持久化操作. 所以
//新的对象中有 id, 但以前的临时对象中没有 id.
@Test
public void testMerge1(){
Customer customer = new Customer();
customer.setAge(18);
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setEmail("cc@163.com");
customer.setLastName("CC");
Customer customer2 = entityManager.merge(customer);
System.out.println("customer#id:" + customer.getId());
System.out.println("customer2#id:" + customer2.getId());
}