我正在尝试使用ORDER BY子句编写JPQL查询:
query = "SELECT c FROM item ORDER BY c.name ASC"
我想设置一个“ order”参数,其值为“ ASC”或“ DESC”:
query = "SELECT c FROM item ORDER BY c.name :order"
然后在我的实现中:
query.setParameter("order", "ASC");
这是当我收到hibernate错误时:
org.hibernate.HibernateException: Errors in named queries
关于我在做什么错的任何想法吗?谢谢!
“ ASC”或“ DESC”不能作为查询参数。您可以改用字符串串联。
query = "SELECT c FROM item ORDER BY c.name " + sortOrder;
您应该验证的内容sortOrder只能是ASC或DESC,而不能直接来自用户。
sortOrder