JPA-entityManager的基本方法

public class JPATest {

    private EntityManager entityManager;
    private EntityTransaction transaction;
    private EntityManagerFactory entityManagerFactory;

    @Before
    public void setUp() throws Exception {
        entityManagerFactory = Persistence.createEntityManagerFactory("jpa-1");
        entityManager = entityManagerFactory.createEntityManager();
        transaction = entityManager.getTransaction();
        transaction.begin();
    }

    @After
    public void destroy() {
        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();
    }
    
    //若传入的是一个游离对象,即传入的对象有OID
    //1.若在entityManager缓存中没有该对象
    //2.若在数据库中有相应的记录
    //3.JPA会查询对应的记录,然后返回该记录的一个对象,再把游离对象的属性复制到查询到的对象中
    //4.对查询到的对象执行update操作
    @Test
    public void testMerge3(){
        Customer customer = new Customer();
        customer.setAge(123);
        customer.setBirthday(new Date());
        customer.setCreattime(new Date());
        customer.setEmail("[email protected]");
        customer.setLastname("bdqn");
        customer.setId(7);
        Customer customer2 = entityManager.merge(customer);
        System.out.println(customer==customer2);
    }
    
    //若传入的是一个游离对象,即传入的对象有OID
    //1.若在entityManager缓存中没有该对象
    //2.若在数据库中也没有相应的记录
    //3.JPA会新建一个对象,然后把游离对象的属性复制给新建对象
    //4.再对新建的对象执行insert操作
    @Test
    public void testMerge2(){
        Customer customer = new Customer();
        customer.setAge(123);
        customer.setBirthday(new Date());
        customer.setCreattime(new Date());
        customer.setEmail("[email protected]");
        customer.setLastname("dami");
        customer.setId(99);
        Customer customer2 = entityManager.merge(customer);
        System.out.println(customer);//null
        System.out.println(customer2);//3
    }
    
    /**
     * 总的来说相当于hibernate中Session的saveorupdate()
     */
    //若传入的是一个临时对象,会 创建一个新的对象,把临时对象的属性复制到新的对象中,
    //然后对新的对象执行持久化操作,所有新的对象有id,原临时对象没有id
    @Test
    public void testMerge1(){//改
        Customer customer = new Customer();
        customer.setAge(123);
        customer.setBirthday(new Date());
        customer.setCreattime(new Date());
        customer.setEmail("[email protected]");
        customer.setLastname("dami");
        Customer customer2 = entityManager.merge(customer);
        System.out.println(customer.getId());//null
        System.out.println(customer2.getId());//3
    }
    
    // 类似于hibernate的Seesion的delete方法,把对象对应的记录从数据库中删除
    //注意:只能删除持久化对象,而hibernate的delete方法还可以删除游离状态
    @Test
    public void testRemvoe(){//删
//        Customer customer = new Customer();
//        customer.setId(2);
        Customer customer = entityManager.find(Customer.class, 3);
        entityManager.remove(customer);
        
    }
    

    // 类似于hibernate的Seesion的save方法
    // 使对象由临时变为持久状态,即执行insert语句
    //和hibernate的save方法的区别:若对象有id,则不能执行insert操作,会抛异常
    @Test
    public void testPersist() {//增
        Customer customer = new Customer();
        customer.setAge(12);
        customer.setBirthday(new Date());
        customer.setCreattime(new Date());
        customer.setEmail("[email protected]");
        customer.setLastname("jiangli");
        //customer.setId(100);
        entityManager.persist(customer);
        System.out.println(customer.getId());
    }
    
    // 类似于hibernate的Seesion的load方法
    //
    @Test
    public void testGetReferenece() {
        Customer customer = entityManager.getReference(Customer.class, 1);
        System.out.println("--------------");
        System.out.println(customer);
    }

    // 类似于hibernate的Seesion的get方法
    // 与getreference()的区别:在调用find()时就已经发送了select语句,
    // 而getreference()属于懒加载,只有在需要时才发送select
    @Test
    public void testFind() {//查
        Customer customer = entityManager.find(Customer.class, 1);
        System.out.println("--------------");
        System.out.println(customer);
    }

}

你可能感兴趣的:(JPA-entityManager的基本方法)