views:

104

answers:

2

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

This will probably work:

You can use strings to define your parameters (this is my method of choice as it is easier to read)

This would look like:

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

...
query.setBoolean("active", is_active);
query.setLong("id", id);
partkyle
@Kyle Partridge is there a reason why your cases don't match for "ID"?
luke
Just a typo. Thanks for correcting me.
partkyle
+2  A: 

Based on https://forum.hibernate.org/viewtopic.php?f=1&amp;t=945694&amp;view=previous

Try adding FROM to the HQL update e.g. UPDATE FROM THING SET ACTIVE = ? WHERE ID = ?. Also, parameters start at 0, as you have discovered. This is not JDBC.

lmz
@Imz wow is that annoying, thanks for tracking this down for me!. i was going insane trying all the different ways to create the query.
luke