尝试使用本机SQL更新数据时出现此错误。这是我的脚本:
update weight_note_receipt set pledge_id =:pledge where wn_id in (:wns)
wns是包含多个wn_id这样的字符串:
wns
wn_id
222,226,228,251,256,262,263,264,265,266,267,272,281,286,294,296,299,301,302,303,306,307,330,332,333,337,338,339,341,368,371,376,377,378,379,380,381,385,391,397,423,424,443,452,454,461,462,463,464,490,503,504,521,525,528,529,530,532,533,549,554,560,561,564,565,566,567,569,570,595,598,600,603,605,606,607,644,646,649,653,661,662,663,667,669,678,683,752,1039,1075,258,259,260,261,268,269,270,287,304,305,308,325,334,604,643,647,648,659,660,664,665,666,704,709,753,754,757,758,809,834,846,861,872,879,882,911,913,916,919,920,164
当我更新(使用query.executeUpdate())时,会引发以下错误:
query.executeUpdate()
请求处理失败;嵌套的异常是org.hibernate.exception.DataException:无法执行本机批量操作查询],根本原因是com.mysql.jdbc.MysqlDataTruncation:数据截断:截断了错误的DOUBLE值:‘222,226,228,251,256,262,263,264,265,266,267,272,281,286,294,296,299,301,303,302,373,302
是因为输入字符串太长?
在数据库中使用参数化查询(预备语句)时,为参数分配值不得更改查询的结构和执行路径(否则数据库不会将其视为参数化查询,并且会引发异常)。
这就是为什么您不能为查询准备语句的原因:
select * from myTable order by ?
select id, f1, ? from myTable
select * from ?
因为为每个参数分配一个值会更改查询的执行路径(请记住,准备好的语句的查询将被解析一次并产生一个执行路径)。
相同的规则适用于Hibernate查询解析器,您不应为参数分配会更改查询结构的值。
将一个带有值的字符串分配1, 2, 3给SHOULD-TO-BE-A-NUMBER参数是相同的,实际上,第一个查询将被翻译为相同,update weight_note_receipt set pledge_id =:pledge where wn_id = :wns但第二个update weight_note_receipt set pledge_id =:pledge where (wn_id = :x1 or wn_id = :x2 or wn_id = :x3)查询将被翻译为,显然,不同的查询具有不同的执行路径。
1, 2, 3
update weight_note_receipt set pledge_id =:pledge where wn_id = :wns
update weight_note_receipt set pledge_id =:pledge where (wn_id = :x1 or wn_id = :x2 or wn_id = :x3)
因此,即使Hibernate没有抛出异常,您的数据库也会抛出异常。