在 Spring 引导中向本机查询添加参数会导致 "org.hibernate.exception.SQLGrammarException",

Adding a parameter to a native query in Spring Boot causes "org.hibernate.exception.SQLGrammarException",

我在 Spring 引导项目中创建了一个本机查询,它在没有参数的情况下工作正常,如下所示:

@Query(nativeQuery = true, value="SELECT system_name as systemName, norcaType as norcaType, "
        + "device_number as deviceNumber,  feature_vector as featureVector, code as norcaCode, count(*) as sum "
        + "FROM (SELECT a.id, a.object_id, a.system_name, b.norca_type AS norcaType, device_number, "
        + "b.feature_vector, a.seqnb, a.object_index, c.code "
        + "FROM system_objectdata a " 
        + "JOIN sick_il_dacq.system_barcode_norca  b "
        + "ON a.id = b.system_objectdata_id " 
        + "AND a.partition_key  = b.partition_key " 
        + "JOIN system_feature_vector c " 
        + "ON b.feature_vector  = c.id " 
        + "JOIN sick_il_services.system_device d " 
        + "ON b.device_number = d.id) detail "  
        + "GROUP BY system_name, device_number, feature_vector")

public List<INorcaSummarySystemDeviceDTO> getNorcaSummaryBySystemAndDeviceAllSystems();

但是,当我添加一个带有单个参数的 where 子句时,它给出了 mysql 语法异常。

这是带有 WHERE 子句和参数的新查询:

@Query(nativeQuery = true, value="SELECT system_name as systemName, norcaType as norcaType, "
        + "device_number as deviceNumber,  feature_vector as featureVector, code as norcaCode, count(*) as sum "
        + "FROM (SELECT a.id, a.object_id, a.system_name, b.norca_type AS norcaType, device_number, "
        + "b.feature_vector, a.seqnb, a.object_index, c.code "
        + "FROM system_objectdata a " 
        + "JOIN sick_il_dacq.system_barcode_norca  b "
        + "ON a.id = b.system_objectdata_id " 
        + "AND a.partition_key  = b.partition_key " 
        + "JOIN system_feature_vector c " 
        + "ON b.feature_vector  = c.id " 
        + "JOIN sick_il_services.system_device d " 
        + "ON b.device_number = d.id"
        + "and a.system_name = ?1 ) detail "  
        + "GROUP BY system_name, device_number, feature_vector")

public List<INorcaSummarySystemDeviceDTO> getNorcaSummaryBySystemAndDeviceOneSystem(String systemName);

我知道原始 SQL 与 where 子句一起工作,因为我可以 运行 它在 MySql Workbench.

我也知道参数正确通过,因为我在调试器中看到它并且它也显示在日志中。

我已经尝试了 named 和 index 变量,每次都得到相同的结果。

知道问题出在哪里吗?

这是日志中打印的错误:

2021/12/21 17:25:47.799 | ERROR | http-nio-8181-exec-6 | o.a.c.c.C.[.[.[.[dispatcherServlet]] | Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a.system_name = '02') detail GROUP BY system_name, device_number, feature_vector' at line 1
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:536)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
    at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)
    at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1826)

您似乎在“ON b.device_number = d.id”这一行的 d.id 之后漏掉了一个 space,请将其更改为“ON b.device_number = d.id”和测试