You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring JPA执行SQL提示','附近语法错误,但数据库中运行正常

问题:Spring JPA执行SQL Server UPDATE语句报语法错误

使用Java 8开发,通过Spring JPA编写的UPDATE查询,在WHERE子句中将job_id、item_nr、whse_nr三列拼接作为条件。该语句直接在SQL Server数据库中执行正常,但通过JPA代码执行时,提示Incorrect syntax near ','语法错误。

JPA代码实现

@Modifying
@Transactional
@Query(value = "UPDATE ListView SET stre_group = :streGroup, method = :mthodSelected, " +
                "alloc_schedule = :allocationSchedule " +
                "WHERE concat(job_id,':',item_nr,':',whse_nr) IN (:resultValue)", nativeQuery = true)
void updateWorklist(String streGroup, String mthodSelected,
                    List<String> allocationSchedule,
                    List<String> resultValue);

数据库可正常执行的示例SQL

UPDATE tablename  SET stre_group = 'store 4', method = 'store 4',
auto_alloc_action = 'store 4', alloc_schedule = 'store 4'
WHERE concat(job_id,':',item_nbr,':',whse_nbr) IN ('1:10:1')

报错信息

ERRO o.h.engine.jdbc.spi.SqlExceptionHelper : Incorrect syntax near ','.
? ERROR 29524 i.m.ca.service.updateDataService: Error occurred while updating records: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement


问题出在这儿,这么修

核心问题是参数类型不匹配:
看你数据库里能跑的SQL,alloc_schedule是单个列,赋值的是单个字符串(比如'store 4'),但JPA方法里对应的allocationSchedule参数写成了List<String>。Hibernate绑定这个列表到单个列的时候,会生成alloc_schedule = ?, ?, ?这种错误语法,自然就报逗号附近的语法错了。

修复方法:

  • 把方法里的List<String> allocationSchedule改成String allocationSchedule,让参数类型和列的赋值需求对上:
@Modifying
@Transactional
@Query(value = "UPDATE ListView SET stre_group = :streGroup, method = :mthodSelected, " +
                "alloc_schedule = :allocationSchedule " +
                "WHERE concat(job_id,':',item_nr,':',whse_nr) IN (:resultValue)", nativeQuery = true)
void updateWorklist(String streGroup, String mthodSelected,
                    String allocationSchedule,
                    List<String> resultValue);

另外再核对下参数名和查询里的占位符,确保拼写一致(当前代码里mthodSelected和:mthodSelected是对的,不用改)。改完之后,生成的SQL就和你数据库里能跑的示例语法一致了,问题应该就解决了。

内容的提问来源于stack exchange,提问作者sharonm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 17:53:12