Case sensitive Search in Hibernate Criteria

后端 未结 5 671
礼貌的吻别
礼貌的吻别 2020-12-09 06:49

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         


        
相关标签:
5条回答
  • 2020-12-09 07:17

    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.

    0 讨论(0)
  • 2020-12-09 07:28
    Criteria criteria = s.createCriteria(User.class);
    criteria.add(
        Expression.sql("userName = ? collate Latin1_General_CS_AS_KS_WS", userName, new StringType())
    );
    
    0 讨论(0)
  • 2020-12-09 07:30

    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.

    0 讨论(0)
  • 2020-12-09 07:39

    You can try this (using ilike in Restrictions API)

    Criteria criteria = s.createCriteria(User.class);   
    criteria.add(Restrictions.ilike("userName", userName));
    

    Regards

    0 讨论(0)
  • 2020-12-09 07:44

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

    0 讨论(0)
提交回复
热议问题