JPA简单的增删改查

创建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();
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值