Follow Slashdot blog updates by subscribing to our blog RSS feed


Forgot your password?
DEAL: For $25 - Add A Second Phone Number To Your Smartphone for life! Use promo code SLASHDOT25. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. Check out the new SourceForge HTML5 Internet speed test! ×
User Journal

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

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

  • 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 %?

Optimism is the content of small men in high places. -- F. Scott Fitzgerald, "The Crack Up"