java - 期待 IDENT,找到 ':'
问题描述
我正在为我的 Spring 项目构建本机查询。有人知道问题是什么吗?
@Query(value = "SELECT e.first_name as firstName, e.last_name as lastName, jh.start_date as startDate, jh.end_date as endDate, " +
"j.job_title as jobName, d.department_name as departmentName FROM JOB_HISTORY jh " +
"JOIN JOBS j ON jh.JOB_ID = j.JOB_ID " +
"JOIN DEPARTMENTS d ON JH.DEPARTMENT_ID = d.DEPARTMENT_ID " +
"JOIN EMPLOYEES e ON jh.EMPLOYEE_ID = e.EMPLOYEE_ID " +
"ORDER BY jh.:sortBy :orderBy")
List<EmployeeJobView> getAllEmployeeJob(String sortBy, String orderBy);
org.hibernate.hql.internal.ast.QuerySyntaxException:期待 IDENT,在第 1 行第 348 列附近找到“:” [选择 e.first_name 作为 firstName,e.last_name 作为 lastName,jh.start_date 作为 startDate,jh.end_date 作为endDate,j.job_title 作为jobName,d.department_name 作为departmentName 从JOB_HISTORY jh JOIN JOBS j ON jh.JOB_ID = j.JOB_ID JOIN DEPARTMENTS d ON JH.DEPARTMENT_ID = d.DEPAR TMENT_ID JOIN EMPLOYEES e ON jh.EMPLOYEE_ID = e.EMPLOYEE_ID ORDER BY jh.:sortBy :orderBy]
解决方案
如果我猜对了,我会使用 SPeL 重写最后一行:
"ORDER BY jh.?#{[0]} ?#{[1]}"
正如Simon Martinelli提到的,您应该使用nativeQuery = true
.
另请参阅此处的 SPeL 与 JPA 指南https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions。
推荐阅读
- docker - Docker Jenkins 无法加载插件
- java - 根据情况启用或禁用按钮的最佳方法
- excel - 使用 VBScrip 将公式替换为 Excel 中的结果值
- c# - 在 C# 中将字节数组转换为图像
- javascript - 如何从JS中的数组中删除特定字符?
- python - 给定两个字符串列表,我如何匹配它们以使尽可能多的字符串相同?
- java - Java,ByteBuffer转换为byte[],array()和get()有什么区别?
- javascript - Javascript append 正在通过控制台将子级添加到父级,但未显示在开发工具元素或页面上
- ios - 导航到 Swift tabBar 时不会刷新视图
- java - Liquibase 忽略 databasechangelog 表内容