hibernate executeUpdate IndexOutOfBounds

时间:2010-06-10 23:31:19

标签: java hibernate hql

我正在尝试使用HQL在hibernate中执行简单的更新,但我似乎无法让它工作。

我的查询模板定义为:

private static final String CHANGE_DEVICE_STATUS
= "UPDATE THING"
 +"SET ACTIVE = ? "
 +"WHERE ID = ?";

然后我尝试执行它:

Session s = HibernateSessionFactory.getSession();
Query query = s.createQuery(CHANGE_DEVICE_STATUS);
query.setBoolean(0, is_active);
query.setLong(1, id);
query.executeUpdate();

但现在我收到了这个错误:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
 at java.util.ArrayList.RangeCheck(ArrayList.java:547)
 at java.util.ArrayList.get(ArrayList.java:322)
 at org.hibernate.hql.ast.HqlSqlWalker.postProcessUpdate(HqlSqlWalker.java:390)
 at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:164)
 at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
 at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
 at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
 at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
 at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
 at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:865)
 at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89)
    ....

我在这里做错了什么?我正在使用hibernate 3.0

更新

我把它改成了

Query query = s.createQuery(CHANGE_DEVICE_STATUS);
query.setBoolean(1, is_active);
query.setLong(2, id);//<---throws here
query.executeUpdate();

除了参数索引之外没有改变任何其他东西,我得到了这个:

java.lang.IllegalArgumentException: Positional parameter does not exist: 2 in query: 
UPDATE DEVICE_INSTANCES SET ACTIVE = ? WHERE DEVICE_INSTANCE_ID = ?
 at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:194)
 at org.hibernate.impl.AbstractQueryImpl.setLong(AbstractQueryImpl.java:244)
    ...

更新2

现在我正在尝试命名参数,所以我将查询更改为此(根据建议)

private static final String CHANGE_DEVICE_STATUS
= "UPDATE DEVICE_INSTANCES "
 +"SET ACTIVE = :active "
 +"WHERE DEVICE_INSTANCE_ID = :id";

我的查询代码为:

Query query = s.createQuery(CHANGE_DEVICE_STATUS);
query.setBoolean("active", is_active);
query.setLong("id", device.getDeviceInstanceId());
query.executeUpdate();

现在我得到了这个异常(在调用executeUpdate时):

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at org.hibernate.hql.ast.HqlSqlWalker.postProcessUpdate(HqlSqlWalker.java:390)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:164)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
    at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
    at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
    at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:865)
    at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89)
    ...

2 个答案:

答案 0 :(得分:2)

基于https://forum.hibernate.org/viewtopic.php?f=1&t=945694&view=previous

尝试将FROM添加到HQL更新中,例如UPDATE FROM THING SET ACTIVE = ? WHERE ID = ?。此外,正如您所发现的,参数从0开始。这不是JDBC。

答案 1 :(得分:0)

这可能会奏效:

您可以使用字符串来定义参数(这是我选择的方法,因为它更容易阅读)

这看起来像是:

private static final String CHANGE_DEVICE_STATUS
= "UPDATE THING"
 +"SET ACTIVE = :active "
 +"WHERE ID = :id";

...
query.setBoolean("active", is_active);
query.setLong("id", id);