hibernate executeUpdate IndexOutOfBounds

Posted by luke on Stack Overflow See other posts from Stack Overflow or by luke
Published on 2010-06-10T23:31:19Z Indexed on 2010/06/10 23:42 UTC
Read the original article Hit count: 235

Filed under:
|
|

I am trying to use an HQL to perform a simple update in hibernate, but i can't seem to get it to work.

i have a query template defined as:

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

and then i try to execute it like this:

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

But now i get this error:

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)
    ....

what am i doing wrong here? I am using hibernate 3.0

UPDATE

i changed it to

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

without changing anything else but the parameter indexes and i got this:

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)
    ...

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate