2010-06-10 64 views
1

我想使用HQL執行一個簡單的更新在休眠,但我似乎無法得到它的工作。休眠executeUpdate IndexOutOfBounds

我有一個查詢模板定義爲:

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

UPDATE

我改成了

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

現在我試着命名參數,所以我改變了這個查詢(作爲PE R上的建議)

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

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

嘗試添加來自於HQL更新例如UPDATE FROM THING SET ACTIVE = ? WHERE ID = ?。同樣,參數從0開始,就像你發現的那樣。這不是JDBC。

+0

@Imz哇,這是煩人的,謝謝你爲我追蹤這件事!我正在瘋狂嘗試所有不同的方式來創建查詢。 – luke 2010-06-11 00:24:53

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); 
+0

Part is是你的個案與「身份證」不符的原因嗎? – luke 2010-06-10 23:39:45

+0

只是一個錯字。謝謝你糾正我。 – partkyle 2010-06-11 00:05:50