I\'m using Hibernate 3 with SQL Server 2008. I want to do case sensitive search in Hibernate Criteria. I\'m not able to achieve it. My code is as below:
Crit
This may not be suitable for you but it may be better to use lucene/solr for these kind of queries. In addition to case insensitive queries it will also handle other common scenarios for you.
Criteria criteria = s.createCriteria(User.class);
criteria.add(
Expression.sql("userName = ? collate Latin1_General_CS_AS_KS_WS", userName, new StringType())
);
I add the same issue, and I changed the collation type in the database table straight in the mariadb server.
I create the table field with "COLLATE latin1_general_cs"
So every search on this field will be case sensitive.
My table field looks like this.
'case_sensitive_key' varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL DEFAULT '',
Hope it will help some of you.
You can try this (using ilike in Restrictions API)
Criteria criteria = s.createCriteria(User.class);
criteria.add(Restrictions.ilike("userName", userName));
Regards
You can't with hibernate. Problem is in MS SQL - by default it use case insensitive collation. There can be two workarounds:
1 Select with hibernate and do program filtering of result
2 When create table use case sensitive collation for filed you nead to search: COLLATE SQL_Latin1_General_CP850_BIN2 or other specified for your server (read from SYSTEMPROPERTY).