创建JPA的工具类JPAUtil
由于EntityManagerFactory是一个线程安全的对象(即多个线程访问同一个EntityManagerFactory对象不会有线程安全问题),并且EntityManagerFactory的创建极其浪费资源,所以在JPA编程时,我们可以对EntityManagerFactory的创建进行优化,只需要做到一个工程只存在一个EntityManagerFactory即可。
package com.wuxudong.utils;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtils {
private static EntityManagerFactory factory;
static {
factory= Persistence.createEntityManagerFactory("myJPA");
}
public static EntityManager getEntityManager(){
return factory.createEntityManager();
}
}
增:persist
//增
@Test
public void testPersist(){
//定义实体类对象
Customer customer=new Customer();
customer.setCustName("吴尼玛");
customer.setCustLevel("VIP");
customer.setCustAddress("南京");
EntityManager em=null;
EntityTransaction entityTransaction=null;
try{
em=JPAUtils.getEntityManager();
entityTransaction=em.getTransaction();
entityTransaction.begin();
em.persist(customer);
entityTransaction.commit();
}catch (Exception e){
entityTransaction.rollback();
e.printStackTrace();
}finally {
em.close();
}
}
改:merge
@Test
public void mergeTest(){
EntityManager em=null;
EntityTransaction entityTransaction=null;
try {
em=JPAUtils.getEntityManager();
entityTransaction=em.getTransaction();
entityTransaction.begin();
Customer customer= em.find(Customer.class,1l);
customer.setCustName("ahfaa");
em.clear();//将customer对象从缓存中清除出去
em.merge(customer);
entityTransaction.commit();
}catch (Exception e){
entityTransaction.rollback();
e.printStackTrace();
}finally {
em.close();
}
}
删:remove
//删除
@Test
public void deleteTest(){
EntityManager em=null;
EntityTransaction entityTransaction=null;
try {
em=JPAUtils.getEntityManager();
entityTransaction=em.getTransaction();
entityTransaction.begin();
Customer customer = em.find(Customer.class,2l);
em.remove(customer);
entityTransaction.commit();
}catch (Exception e){
entityTransaction.rollback();
e.printStackTrace();
}finally {
em.close();
}
}
根据id查询
//查询:根据id查询
@Test
public void testFindById(){
EntityManager em=null;
EntityTransaction entityTransaction=null;
try {
em=JPAUtils.getEntityManager();
entityTransaction=em.getTransaction();
entityTransaction.begin();
Customer customer=em.find(Customer.class,1L);
System.out.println(customer);
entityTransaction.commit();
}catch (Exception e){
entityTransaction.rollback();
e.printStackTrace();
}finally {
em.close();
}
}
查询实体的缓存问题
EntityManager中存在缓存
//查询实体的缓存问题
@Test
public void testGetOne(){
//定义对象
EntityManager em=null;
EntityTransaction entityTransaction=null;
try {
//获取实体管理对象
em=JPAUtils.getEntityManager();
entityTransaction=em.getTransaction();
entityTransaction.begin();
Customer c1=em.find(Customer.class,1l);
Customer c2=em.find(Customer.class,1l);
System.out.println(c1==c2);//true,EntityManager也有缓存
entityTransaction.commit();
}catch (Exception e){
entityTransaction.rollback();
e.printStackTrace();
}finally {
em.close();
}
}
带有延迟加载的查询
@Test
public void testLoadOne(){
//定义对象
EntityManager em=null;
EntityTransaction entityTransaction=null;
try {
em=JPAUtils.getEntityManager();
entityTransaction=em.getTransaction();
entityTransaction.begin();
Customer c1=em.getReference(Customer.class,1l);
entityTransaction.commit();
System.out.println(c1);
}catch (Exception e){
entityTransaction.rollback();
e.printStackTrace();
}finally {
em.close();
}
}