****************JPA API****************
一:章节内容
使用:
###EntityManager:
方法:find使用
package com.atguigu.jpa.test; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.atguigu.jpa.helloworld.Customer; public class JPATest { private EntityManagerFactory entityManagerFactory; private EntityManager entityManager; private EntityTransaction transaction ; @Before public void init(){ entityManagerFactory = Persistence.createEntityManagerFactory("Jpa-01"); entityManager = entityManagerFactory.createEntityManager(); transaction = entityManager.getTransaction(); transaction.begin(); } @After public void destory(){ transaction.commit(); entityManager.close(); entityManagerFactory.close(); } //类似于hibernate中session的get方法 @Test public void testFind(){ Customer customer = entityManager.find(Customer.class, 1); System.out.println("****************************"); System.out.println(customer); } }
打印结果:
方法:getReference
@Test public void testGetReference(){ Customer customer = entityManager.getReference(Customer.class, 1);
System.out.println("-------------------------------------------"); System.out.println(customer); }
打印:
这种方法可能会导致懒加载异常。
方法:Persistence
//类似于hibernate中的save方法,是对象由临时状态变为持久化状态 //和hibernate的save不同之处是:若对象有id,则不能执行insert操作,而会抛出异常 @Test public void testPersistence(){ Customer customer = new Customer(); customer.setAge(15); customer.setBirth(new Date()); customer.setCreatedDate(new Date()); customer.setEmail("110.emal"); customer.setLastName("aaaaa"); entityManager.persist(customer); System.out.println(customer.getId()); }
方法:remove
//类似于hibernate中session的delete方法 //把对象对应的记录从数据库中移除, //但注意:该方法只能移除持久化对象,而hibernate中的delete方法实际上还可以移除游离对象 @Test public void testRemove(){ /*Customer customer = new Customer(); customer.setId(2);*/ Customer customer = entityManager.find(Customer.class, 2); entityManager.remove(customer); }
方法:merge
解析图:
情况一:
/** * 总的来说:类似于hibernate Session 的saveOrUpdate方法 */ //1.若传入的是一个临时对象 //会创建一个新的对象,把临时对象的属性复制到新的对象中,然后对新的对象执行持久化操作, //所以新的对象中有id,但以前的临时对象中没有id @Test public void testMerge1(){ Customer customer = new Customer(); customer.setAge(152); customer.setBirth(new Date()); customer.setCreatedDate(new Date()); customer.setEmail("88888.emal"); customer.setLastName("aaccccc"); Customer customer2 = entityManager.merge(customer); System.out.println("customer#id"+customer.getId()); System.out.println("customer2#id"+customer2.getId()); }
输出:
情况2:
//若传入的是一个游离对象,即传入的对象有OID, //1.若在EntityManager缓存中没有该对象 //2.若在数据库中也没有对应的记录 //3.JPA会创建一个新的对象,然后把当前游离对象的属性复制到新创建的对象中 //4.对新创建的对象执行insert操作 @Test public void testMearge2(){ Customer customer = new Customer(); customer.setAge(188); customer.setBirth(new Date()); customer.setCreatedDate(new Date()); customer.setEmail("dddddd.emal"); customer.setLastName("ddddddd"); customer.setId(100); Customer customer2 = entityManager.merge(customer); System.out.println("customer#id"+customer.getId()); System.out.println("customer2#id"+customer2.getId()); }
输出:
情况3:
//若传入的是一个游离对象,即传入的对象有OID, //1.若在EntityManager缓存中没有该对象 //2.若在数据库中有对应的记录 //3.JPA会查询对应的记录,然后返回该记录对应的一个对象, // 再然后会把有理对象的属性复制到查询到的对象中 //4.对查询到的对象执行update操作 @Test public void testMearge3(){ Customer customer = new Customer(); customer.setAge(188); customer.setBirth(new Date()); customer.setCreatedDate(new Date()); customer.setEmail("eeeee.emal"); customer.setLastName("eeeeee");//可以通过setName方法调用几次查看效果 customer.setId(4);//执行之前数据库中已经有id为4 的数据 Customer customer2 = entityManager.merge(customer); System.out.println(customer == customer2); }
打印:
情况4:
//若传入的是一个游离对象,即传入的对象有OID, //1.若在EntityManager缓存中有该对象 //2.JPA会把游离对象的属性复制到查询到的EntityManager缓存中的对象中 //3.EntityManager缓存中的对象执行update @Test public void testMearge4(){ Customer customer = new Customer(); customer.setAge(188); customer.setBirth(new Date()); customer.setCreatedDate(new Date()); customer.setEmail("dddd.emal"); customer.setLastName("dddd"); customer.setId(4); Customer customer2 = entityManager.find(Customer.class, 4); entityManager.merge(customer); System.out.println(customer == customer2); }
打印:
其他方法:
方法:flush
/** * 同hibernate中的session的flush方法 */ @Test public void testFlush(){ Customer customer = entityManager.find(Customer.class, 1); System.out.println(customer); customer.setLastName("AA"); entityManager.flush();//此处会显示更新语句,但没有提交事务,事务提交是在commit方法中 }
方法:refresh