JPA 中原生find()方法和getreference()方法的使用

Customer customer = em.find(Customer.class,2);

报错:java.lang.IllegalArgumentException: Provided id of the wrong type for class com.blade.day1.domain.Customer. Expected: class java.lang.Long, got class java.lang.Integer
传入的参数类型应该是longl类型

 		//通过实体管理类工厂获取实体管理器
        EntityManager em = JpaUtils.getEntityManager();
        //获取事务对象,开启事务
        EntityTransaction tx = em.getTransaction();//获取事务对象
        tx.begin();//开启事务
        //完成增删改操作
        /**
         * find:根据ID查询:
         *      1: class查询数据的结果需要包装的实体类类型的字节码
         *      2: id 查询的主键的值 要注意的是,id的值默认是long类型,
         * 		所以应该传入long类型的值
         *  立即查询  find方法执行的时候,就会发送sql查询数据库
         */
        Customer customer1 = em.find(Customer.class,2L);
        System.out.println("customer = " + customer1);
         /**
         * getReference:根据ID查询:
         *      1: class查询数据的结果需要包装的实体类类型的字节码
         *      2: id 查询的主键的值
         *   懒加载:
         *      * 得到的是一个动态代理对象
         *      * 什么时候用,什么时候会加载
         */
        Customer customer2 = em.getReference(Customer.class,1L);
        System.out.println("customer = " + customer2);
        // 提交事务
        tx.commit();
        // 释放资源
        em.close();
发布了8 篇原创文章 · 获赞 1 · 访问量 111

猜你喜欢

转载自blog.csdn.net/weixin_44218060/article/details/103793392
今日推荐