0

I'm new in Hibernate. I can't understand how Hibernate process ? in Restrictions.sqlRestriction() method. For example, there is a sample from official guide:

List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.sqlRestriction("lower({alias}.name) like lower(?)", "Fritz%", Hibernate.STRING) )
    .list();

Looks like it is a placeholder. Will Hibernate convert this statement in ...like lower(Fritz%) SQL request?

If yes, then lower(Fritz%) doesn't looks valid, correct?

Adam Shakhabov
  • 1,194
  • 2
  • 14
  • 35

1 Answers1

2

Yes, ? is a placeholder that is filled with "Fritz%".

% it's a wildcard, and it is often used with like like in this case.
Basically the query search for every Cat whose name start with "Fritz".

EDIT
I didn't get what was your concern with lower(Fritz%) at first, but as XtremeBaumer suggested, the query is actually converted to lower('Fritz%')

Kaiak
  • 88
  • 7
  • 3
    It is converted to `lower('Fritz%')`. You are missing the `'` signs – XtremeBaumer May 13 '22 at 07:49
  • then SQL statement `... like lower('Fritz%')` searches for every Cat whose name starts with "Fritz" and converts it into lower-case, right? – Adam Shakhabov May 13 '22 at 08:12
  • The other way: the ```lower``` (in both case) is applied before the ```like```, so the query is actually case insensitive. The query is going to match all the cats whose name start with "felix", "Felix", "FeLiX", etc. – Kaiak May 13 '22 at 08:19
  • 1
    @Kaiak – You surely mean "fritz", "FrItZ", "FRITZ", … and not "Felix", right? – tquadrat May 13 '22 at 08:29
  • @tquadrat yes of course, sorry! – Kaiak May 13 '22 at 08:53
  • @XtremeBaumer In most SQL implementations it will not convert to `lower('Fritz%')`, the statement is prepared with a parameter, and on execute, a value is sent to the server for that parameter. The value is usually not inlined into the query string. – Mark Rotteveel May 13 '22 at 09:05