当前位置 : 主页 > 手机开发 > ROM >

setParameter不支持传统的按位置查询方式

来源:互联网 收集:自由互联 发布时间:2021-06-10
setParameter不支持传统的按位置查询方式 String hql = "from Customer as c where c.cust_id = ?"; ListCustomer list = session.createQuery(hql).setParameter(0, 2l).list(); for (Customer customer : list) { System.out.println(customer

setParameter不支持传统的按位置查询方式

String hql = "from Customer as c where c.cust_id = ?";
        List<Customer> list = session.createQuery(hql).setParameter(0, 2l).list();
        for (Customer customer : list) {
            System.out.println(customer);
        }

出现错误

Legacy-style query parameters (`?`) are no longer supported; 
use JPA-style ordinal parameters (e.g., `?1`) instead : 
from com.hibernate.domain.Customer as c where c.cust_id = ? 
[from com.hibernate.domain.Customer as c where c.cust_id = ?]

更改方法

?的后面加个整数的数,然后在使用setParameter的时候使用当时的添加的整数进行绑定

String hql = "from Customer as c where c.cust_id = ?1";
        List<Customer> list = session.createQuery(hql).setParameter(1, 2l).list();
        for (Customer customer : list) {
            System.out.println(customer);
        }
网友评论