我想使用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)
...
@Imz哇,這是煩人的,謝謝你爲我追蹤這件事!我正在瘋狂嘗試所有不同的方式來創建查詢。 – luke 2010-06-11 00:24:53