在JPA中,可以使用以下几种方式解决query参数问题:
String queryString = "SELECT e FROM Employee e WHERE e.salary > :salary";
TypedQuery<Employee> query = entityManager.createQuery(queryString, Employee.class);
query.setParameter("salary", 50000);
List<Employee> employees = query.getResultList();
String queryString = "SELECT e FROM Employee e WHERE e.salary > ?1";
TypedQuery<Employee> query = entityManager.createQuery(queryString, Employee.class);
query.setParameter(1, 50000);
List<Employee> employees = query.getResultList();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
Root<Employee> root = cq.from(Employee.class);
cq.select(root).where(cb.gt(root.get("salary"), 50000));
TypedQuery<Employee> query = entityManager.createQuery(cq);
List<Employee> employees = query.getResultList();
无论使用哪种方式,都可以解决JPA中的query参数问题,选择最适合自己情况的方式即可。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: jpa delete删除数据未生效怎么解决