1

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.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • What if you drop the parameter and just execute `"from entity... with childentityid=" + someVar`? – matt b May 16 '11 at 15:30
  • 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 May 16 '11 at 15:42
  • 3
    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 May 16 '11 at 15:46

4 Answers4

1

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

from entity as vv inner join vv.childentity as vis with vis.childentityid=?"
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • No unfortunately I already tried this. If I do this or try to filter on the id in a WHERE clause I get a could not resolve property childentityid in childentity. – maple_shaft May 16 '11 at 15:40
  • 1
    @maple_shaft: Does that property actually exist? If no, what do you try to express in `with` clause? – axtavt May 16 '11 at 15:48
  • Yes, the property exists, it is the unique identifier of childentity. My hope is to INNER JOIN entity and childentity, constraining childentity objects to those that have the id passed as the parameter. – maple_shaft May 16 '11 at 16:04
1

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.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
0

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.

H.Tries
  • 23
  • 6
-3

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

zach
  • 7
  • 1
    parameter starts from zero and not from one.. here is javadocs - http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/Query.html#setParameter(int,java.lang.Object\) – Premraj May 16 '11 at 15:26