Forgot your password?
typodupeerror
User Journal

FortKnox's Journal: [Java/Hib3/EJB3] HQL 'like' clauses.... 4

Journal by FortKnox
Ugh, I just spent hours of research to find something. I know this only pertains to <5 people that read this, but I wanted it to be available to the rest of the net in case someone else has the same issue:
If you are using a 'like' clause in hql (I use spring's hibernate template), this is wrong:

getHibernateTemplate().find("from Upc upc where upc.upcNumber like '?%'", new Object[]{param});

HQL doesn't like the look of %'s in its queries... so you have to change it to:

getHibernateTemplate().find("from Upc upc where upc.upcNumber like ?", new Object[]{param + "%"});

The error message I got was out in left field:

IndexOutOfBoundsException: Remember that ordinal parameters are 1-based

REALLLL helpful, there.... sheesh...

This discussion has been archived. No new comments can be posted.

[Java/Hib3/EJB3] HQL 'like' clauses....

Comments Filter:
  • one of the reasons i like ibatis (over hibernate) is the simplicity of it. hibernate is just, well, its just weird. ibatis simply provides for, what i think, is a much more elegant interface for modeling data, and well, if you want to use a standard LIKE statement, then fine, go right ahead.
  • but I wanted it to be available to the rest of the net

    Slashdot journals are not indexed by search engines (well, the ones that obey robots.txt, anyway, which are the ones anybody actually uses (and I expect that any spider that doesn't is quickly IP-banned)).
  • by robi2106 (464558)
    we had some odd problems doing nested queries in our HQL, but we stuck the actual HQL in the object's *.hbm.xml so our Java action only has to reference the query name.

    jason
  • I'm sure the problem is that the ? is the same as the PreparedStatement functionality in regular Java.

    Wouldn't you be better off just appending the % to whatever string you're passing in instead of force fitting the %?

Money is the root of all evil, and man needs roots.

Working...