首页 > 解决方案 > 期待 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]

标签: javaspringhibernatejpa

解决方案


如果我猜对了,我会使用 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


推荐阅读