Search code examples
javahibernatehql

Getting Invalid parameter index 1 from simple HQL query


I have the following HQL query and for simplicity sake lets assume the mappings and table names are correct.

String queryString = "from entity as vv inner join vv.childentity as vis with childentityid=?";
Query query = session.createQuery(queryString);
query.setParameter(0, someVarId);
List<entity> entities = query.list();

I get the following error when attempting to execute this:

ERROR: could not bind value '12' to parameter: 1; Invalid parameter index 1.

I suspect this might be because HQL implicitly does not support binding parameters in the WITH clause. I cannot find any documentation saying that this is not supported and I RTFM.

Can anybody confirm this is true or that this is a known Hibernate bug, or a good workaround would be nice too.

EDIT: I forgot to mention that I get the same error even if using a named parameter.


Solution

  • Thanks for your help but I figured out the weirdness.

    When I am joining two objects in HQL it should be done this way.

    from entity as vv where childentityid=?
    

    I found out that I don't actually need to join them, I wasn't giving HQL enough credit to look at the object mappings and determine that entity has a property called childentity and thus childentityid is the unique identifier of it.

    Thank you for all of your help.