RaMs_YearnsToLearn
RaMs_YearnsToLearn

Reputation: 509

Retrieve french characters from database in java

I've the sample input as "Mickaël"
When I hit the database, to retrive values by adding criteria in hibernate code snippet is as follows.

pCriteria.add(Restrictions.ilike("lastName", lLastName.toLowerCase() + "%"));

I get the result only with "mickaël".

But as of my Requirement I need to fetch both "Mickaël" and "Mickael"

can somebody help out with this??? TIA

Upvotes: 1

Views: 1037

Answers (2)

neshkeev
neshkeev

Reputation: 6476

Try like this:

pCriteria.add(Restrictions.ilike("lastName", lLastName.toLowerCase().replaceAll('[ë]', '_')));

I am trying to make this string Micka_l. The sign '_' stands for any character, so if you have something like Micka^l or Mickaql in your table you will see this records too. You can add more special french characters in square brackets like this [ëöèù] to escape other special characters.

If you don't want to have other characters try this:

pCriteria.add(pCriteria.or(
    Restrictions.eq("lastName", lLastName.toLowerCase())), 
    Restrictions.eq("lastName", lLastName.toLowerCase().replaceAll('[ë]', 'e'))
  )
);

I assume that you input the whole word, if you don't change eq to ilike and add percent sign at the ends of the lines, like this:

pCriteria.add(pCriteria.or(
    Restrictions.ilike("lastName", lLastName.toLowerCase() + "%")), 
    Restrictions.ilike("lastName", lLastName.toLowerCase().replaceAll('[ë]', 'e') + "%")
  )
);

If there is another characters to escape you have to work around them like above.

Upvotes: 0

alain.janinm
alain.janinm

Reputation: 20065

You have to set a collation for example COLLATE French_CI_AI, either on the table or the query (see this post)

Upvotes: 1

Related Questions