HIbernate查询参数绑定

时间:2013-04-18 08:59:09

标签: hibernate

我想通过hibernate执行本机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在字符串中嵌入paraneters,那么它可以正常工作!

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

谢谢!

1 个答案:

答案 0 :(得分:0)

balanceType不是参数,因此您无法绑定它,您需要使用字符串连接来连接列名。

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