1
votes

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.

4
What if you drop the parameter and just execute "from entity... with childentityid=" + someVar? - matt b
It works, but then of course if I do this I leave myself open to injection attacks. That is not an option unfortunately. - maple_shaft
since HQL is parsed by Hibernate anyway, is there really an opening for injection attacks? As an alternative, what about from entity inner join childentity where childentity.childentityid = ? - matt b

4 Answers

1
votes

I guess you need to use full name in with clause:

from entity as vv inner join vv.childentity as vis with vis.childentityid=?"
1
votes

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.

0
votes

Not directly related to your exact problem but I came to this thread by search engine.

Had same error 'Invalid parameter index 1' and have two hints for it:

  1. For all coming from simple java.sql. zach is right - you have to start counting by 1. For JBoss/HBL you have to begin to count by 0.
  2. My actual mistake was that I used quotation around the placeholder. (e.g. "SELECT foo FROM bar WHERE foobar like '?';")

As already mentioned - my answer is to clarify this Thread in case you come from simple java.sql.

-3
votes

query.setParameter(0, someVarId) needs to be: query.setParameter(1, someVarId)