EntityManager#merge

merge() 用于处理 Entity 的同步。即数据库的插入和更新操作

merge的几种情况

4、JPA-EntityManager.merge()-LMLPHP

1. 若传入的是一个临时对象

package jpa.test;

import com.jpa.entity.Customer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.Date; public class MergeTest {
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
private EntityTransaction transaction; @Before
public void init() {
entityManagerFactory = Persistence.createEntityManagerFactory("jpaname");
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
transaction.begin();
} @After
public void destroy() {
transaction.commit();
entityManager.close();
entityManagerFactory.close();
} // 1. 若传入的是一个临时对象
// 会创建一个新的对象, 把临时对象的属性复制到新的对象中, 然后对新的对象执行持久化操作
// 所以新的对象中有 id, 但以前的临时对象中没有 id
@Test
public void testMerge1() {
Customer customer = new Customer();
customer.setAge(18);
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setEmail("[email protected]");
customer.setLastName("CC"); Customer customer2 = entityManager.merge(customer); System.out.println("customer#id:" + customer.getId());
System.out.println("customer2#id:" + customer2.getId());
}
}

4、JPA-EntityManager.merge()-LMLPHP

2.若传入的是一个游离对象, 即传入的对象有 OID

// 2.若传入的是一个游离对象, 即传入的对象有 OID
// 若在 EntityManager 缓存中没有该对象
// 若在数据库中也没有对应的记录
// JPA 会创建一个新的对象, 然后把当前游离对象的属性复制到新创建的对象中
// 对新创建的对象执行 insert 操作
@Test
public void testMerge2() {
Customer customer = new Customer();
customer.setAge(18);
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setEmail("[email protected]");
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());
}

4、JPA-EntityManager.merge()-LMLPHP

3.若传入的是一个游离对象, 即传入的对象有 OID,在数据库中有对应的记录

4、JPA-EntityManager.merge()-LMLPHP

// 3.若传入的是一个游离对象, 即传入的对象有 OID,在数据库中有对应的记录
// 在 EntityManager 缓存中没有该对象
// JPA 会查询对应的记录, 然后返回该记录对一个的对象, 再然后会把游离对象的属性复制到查询到的对象中
// 对查询到的对象执行 update 操作
@Test
public void testMerge3(){
Customer customer = new Customer();
customer.setAge(18);
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setEmail("[email protected]");
customer.setLastName("EE"); customer.setId(4); Customer customer2 = entityManager.merge(customer); System.out.println(customer == customer2); //false
}

4、JPA-EntityManager.merge()-LMLPHP

4、JPA-EntityManager.merge()-LMLPHP

4.若传入的是一个游离对象, 即传入的对象有 OID,在 EntityManager 缓存中有对应的对象

// 4.若传入的是一个游离对象, 即传入的对象有 OID,在 EntityManager 缓存中有对应的对象
// JPA 会把游离对象的属性复制到查询到 EntityManager 缓存中的对象中
// EntityManager 缓存中的对象执行 UPDATE
@Test
public void testMerge4(){
Customer customer = new Customer();
customer.setAge(18);
customer.setBirth(new Date());
customer.setCreatedTime(new Date());
customer.setEmail("[email protected]");
customer.setLastName("DD"); customer.setId(4);
Customer customer2 = entityManager.find(Customer.class, 4); entityManager.merge(customer); System.out.println(customer == customer2); //false
}

4、JPA-EntityManager.merge()-LMLPHP

4、JPA-EntityManager.merge()-LMLPHP

05-11 16:03