06-根据id查询客户

    @Test
    public void testFind(){
        //1、通过工具类获取entityManager
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2、开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        /*
        3、增删改查---根据id查询客户
            find:根据id查询客户
                class:查询数据的结果需要包装的实体类类型的字节码
                id:查询的主键的取值
         */
        Customer customer = entityManager.find(Customer.class, 1l);
        System.out.println(customer);
        //4、提交事务
        tx.commit();
        //5、释放资源
        entityManager.close();
    }
@Test
    public void testReference(){
        //1、通过工具类获取entityManager
        EntityManager entityManager = JpaUtils.getEntityManager();
        //2、开启事务
        EntityTransaction tx = entityManager.getTransaction();
        tx.begin();
        /*
        3、增删改查---根据id查询客户
            getReference:根据id查询客户
                class:查询数据的结果需要包装的实体类类型的字节码
                id:查询的主键的取值
         */
        Customer customer = entityManager.getReference(Customer.class, 1l);
        System.out.println(customer);
        //4、提交事务
        tx.commit();
        //5、释放资源
        entityManager.close();
    }
上一篇:Legacy autograd function with non-static forward method is deprecated and will be removed in 1.3.


下一篇:界面跳转报错 Cannot forward after response has been committed