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)
...
UPDATE 2
Now I'm trying named parameters, so i changed the query to this (as per the suggestion)
private static final String CHANGE_DEVICE_STATUS
= "UPDATE DEVICE_INSTANCES "
+"SET ACTIVE = :active "
+"WHERE DEVICE_INSTANCE_ID = :id";
and my query code to:
Query query = s.createQuery(CHANGE_DEVICE_STATUS);
query.setBoolean("active", is_active);
query.setLong("id", device.getDeviceInstanceId());
query.executeUpdate();
and now i get this exception (on the call to 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)
...