The code we use is straightforward in this part of the search query:
myCriteria.Add( Expression.InsensitiveLike("Code", itemCode, MatchMode.Anywhere));
and it works great in a production environment.
The problem is that one of our customers has product codes containing% characters that this request must match. The resulting SQL output from this code is similar to:
SELECT ... FROM ItemCodes WHERE ... AND Code LIKE '%ItemWith%Symbol%'
which clearly explains why they get some odd results when searching for items.
Is there a way to enable escaping using Criteria programming methods?
Application:
We are using the slightly old version of NHibernate, 2.1.0.4000 (the current one since writing is 2.1.2.4853), but I checked the release notes and there was no mention of a fix for this. I also did not find an open problem in my bugtracker.
We use SQL Server, so I can easily escape the special characters (%, _, [, and ^) in the code, but the point of using NHibernate was to make our database as database independent as possible .
Neither Restrictions.InsensitiveLike() nor HqlQueryUtil.GetLikeExpr() go out of their inputs, and removing the MatchMode parameter MatchMode n't matter as much as possible.
Update: I found someone else who wanted to do the same (three years ago), and the permission was to add escapeChar overloads to the methods described above (this was fixed in version 2.0.0.3347). I added a comment on this issue, requiring further resolution.