使用jpa的简单的增删查找操作

保存

  @Test
    public void  testAdd(){
        Customer c = new Customer();
        c.setCustName("船只");
        c.setCustLevel("VIP");
        c.setCustSource("网络");
        c.setCustIndustry("It");
        c.setCustAddress("胡安娜");
        c.setCustPhone("15512234556");

        EntityManager em = null;
        EntityTransaction tx = null;

        try {
            // 获取实体管理对象
            em = JPAUtil.getEntityManager();
            // 获取事务对象
            tx = em.getTransaction();
            // 开启事务
            tx.begin();
            // 执行操作
            em.persist(c);
            // 提交事务
            tx.commit();
        } catch (Exception e) {
            // 回滚事务
            tx.rollback();
            e.printStackTrace();
        } finally {
            // 释放资源
            em.close();
        }
    }

修改

    @Test
    public void  testMerge(){
        EntityManager em = null;
        EntityTransaction tx = null;

        try {
            em = JPAUtil.getEntityManager();
            tx = em.getTransaction();
            tx.begin();

            // 执行操作
            // 必修6l,java.lang.IllegalArgumentException: Provided id of the wrong type for class com.oceanstar.domain.Customer.
            Customer c1 = em.find(Customer.class, 6l);
            c1.setCustName("执行修改操作");
            em.clear(); //把c1对象从缓存中清除出去
            em.merge(c1);
            tx.commit();
        }catch (Exception e){
            tx.rollback();
            e.printStackTrace();
        }finally {
            em.close();
        }
    }

删除

	/**
	 * 删除
	 */
	@Test
	public void testRemove() {
		// 定义对象
		EntityManager em = null;
		EntityTransaction tx = null;
		try {
			// 获取实体管理对象
			em = JPAUtil.getEntityManager();
			// 获取事务对象
			tx = em.getTransaction();
			// 开启事务
			tx.begin();
			// 执行操作
			Customer c1 = em.find(Customer.class, 6L);
			em.remove(c1);
			// 提交事务
			tx.commit();
		} catch (Exception e) {
			// 回滚事务
			tx.rollback();
			e.printStackTrace();
		} finally {
			// 释放资源
			em.close();
		}
	}

根据id查询

查询一个:使用立即加载策略

find() 方法:根据id查询客户
在这里插入图片描述

// 查询实体的缓存问题
	@Test
	public void testGetOne() {
		// 定义对象
		EntityManager em = null;
		EntityTransaction tx = null;
		try {
			// 获取实体管理对象
			em = JPAUtil.getEntityManager();
			// 获取事务对象
			tx = em.getTransaction();
			// 开启事务
			tx.begin();
			// 执行操作
			Customer c1 = em.find(Customer.class, 1L);
			Customer c2 = em.find(Customer.class, 1L);
			System.out.println(c1 == c2);// 输出结果是true,EntityManager也有缓存
			// 提交事务
			tx.commit();
			System.out.println(c1);
		} catch (Exception e) {
			// 回滚事务
			tx.rollback();
			e.printStackTrace();
		} finally {
			// 释放资源
			em.close();
		}
	}

在这里插入图片描述

查询一个:延迟加载

 @Test
    public void  testFindOne(){
        EntityManager em = null;
        EntityTransaction tx = null;

        try {
            em = JPAUtil.getEntityManager();
            tx = em.getTransaction();
            tx.begin();
            Customer c1 = em.getReference(Customer.class, 2l);
            Customer c2 = em.getReference(Customer.class, 2l);
            tx.commit();
            System.out.println(c1 == c2);
        }catch (Exception e){
            tx.rollback();
            e.printStackTrace();
        }finally {
            em.close();
        }
    }

在这里插入图片描述
在这里插入图片描述

发布了254 篇原创文章 · 获赞 70 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/zhizhengguan/article/details/104007749