0

我想通过休眠执行本机 SQL 查询:

dbs.createSQLQuery("UPDATE USERS SET :balanceType = :newBalance WHERE UKEY = :ukey")
    .setString("balanceType", type.toString())
    .setBigDecimal("newBalance", newBalance)
    .setLong("ukey", uKey).executeUpdate();

由于 :balanceType 绑定,此操作失败。

我得到了例外:

org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query
org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query
java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification

org.hibernate.exception.NestableDelegate@431d9f05
could not execute native bulk manipulation query
UPDATE USERS SET :balanceType = :newBalance WHERE UKEY = :ukey
java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification

如果我使用 String.format 在字符串中嵌入参数,那么它工作正常!

知道我的语法有什么问题吗?

谢谢!

4

1 回答 1

0

balanceType不是参数,所以不能绑定,需要使用字符串连接来连接列名。

dbs.createSQLQuery("UPDATE USERS SET " + type.toString() + " = :newBalance WHERE UKEY = :ukey")
    .setBigDecimal("newBalance", newBalance)
    .setLong("ukey", uKey).executeUpdate();
于 2013-04-18T09:02:44.470 回答