带参数的Spring Data本机查询不起作用 编辑:

问题描述

我有一个本地查询,如下所示:

@Query(value = "SELECT * FROM (" +
        "    SELECT result.*,ROWNUM rn FROM (" +
        "        SELECT tmp.* FROM (" +
        "            SELECT " +
        "                e.id," +
        "                e.employee_number," +
        "                d.name," +
        "                d.surname " +
        "            FROM employee e INNER JOIN detail d ON e.id_detail = d.id " +
        "            WHERE e.status = :status " +
        "        ) tmp " +
        "        ORDER BY :sortColumn :sortDirection " +
        "    ) result " +
        "    WHERE ROWNUM <= (:pageIndex + :pageSize) " +
        ") " +
        "WHERE rn > :pageIndex ",nativeQuery = true)
ArrayList<Object> getEmployeeDetails( @Param("status") EmployeeStatus status,@Param("pageSize") int pageSize,@Param("pageIndex") int pageIndex,@Param("sortDirection") String sortDirection,@Param("sortColumn") String sortColumn);

我遇到以下错误

org.springframework.dao.InvalidDataAccessResourceUsageException: Could not extract ResultSet; sql [n/a]; nested exception is org.hibernate.exception.sqlGrammarException: Could not extract ResultSet
// ...
Caused by: org.hibernate.exception.sqlGrammarException: Could not extract ResultSet
// ...
Caused by: java.sql.sqlSyntaxErrorException: ORA-01745: invalid host/bind variable name

我尝试的是不同的返回类型(并且最终没有找到要使用的返回类型),并插入带有@param()批注的参数。
查询本身确实可以工作-我直接在数据库中尝试了该查询,但是在Spring中处理该查询时遇到了问题。

查询本身,以便于调试:

SELECT * FROM (
    SELECT result.*,ROWNUM rn FROM (
        SELECT tmp.* FROM (
            SELECT
                e.id,e.employee_number,d.name,d.surname
            FROM employee e INNER JOIN detail d ON e.id_detail = d.id
            WHERE e.status = 'status'
        ) tmp
        ORDER BY tmp.name desc
    ) result
    WHERE ROWNUM <= (0 + 5)
)
WHERE rn > 0

编辑:

我已经用删除所有\n的注释建议来更新该问题,并检查是否缺少空格。

不使用任何参数的普通查询也可以工作,但是当我开始通过@param()批注或绑定(?1)插入参数时,它将停止工作,并给出我上面更新的错误

解决方法

如果EmployeeStatusenum,则必须在查询中使用它

WHERE e.status = :#{#status.name()}