hibernate executeUpdate IndexOutOfBounds

。_饼干妹妹 提交于 2019-12-13 02:38:42

问题


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

回答1:


Based on https://forum.hibernate.org/viewtopic.php?f=1&t=945694&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.




回答2:


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


来源:https://stackoverflow.com/questions/3019281/hibernate-executeupdate-indexoutofbounds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!