区分大小写在Hibernate Criteria中搜索 [英] Case sensitive Search in Hibernate Criteria

查看:371
本文介绍了区分大小写在Hibernate Criteria中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server 2008中使用Hibernate 3.我想在Hibernate Criteria中进行区分大小写的搜索。我无法实现它。我的代码如下:

  Criteria criteria = session.createCriteria(User.class); / *第1行* / 
criteria.add(Restrictions.eq(userName,userName)); / *第2行* /

我有一个 User.java 类包含字符串userName



数据库条目:

  id | user_name 
--------------
1 | abc
2 | xyz

现在,如果我传递abc作为第2行中的 userName ,那么它应该从db中返回第一条记录。但如果我通过AbcABCaBC等作为 userName 在第2行中,不应该提取任何记录。



我访问过这个链接,但它对我没有帮助,因为我不想使用hql或SQL Server的排序规则。我很乐意使用 Criteria 的排序规则,但不知道如何去做。

解决方案

  Criteria criteria = s.createCriteria(User.class); 
criteria.add(
Expression.sql(userName =?collat​​e Latin1_General_CS_AS_KS_WS,userName,new StringType())
);


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:

Criteria criteria = session.createCriteria(User.class); /*line 1*/
criteria.add(Restrictions.eq("userName", userName));    /*line 2*/

I have a class User.java which contains a String userName.

DB Entries:

id | user_name
--------------
1  | abc
2  | xyz

Now, if I pass "abc" as userName in line 2, then it should return the first record from the db. But if I pass "Abc", "ABC", "aBC" etc. as userName in line 2, no records should be fetched.

I've visited this link but it's not helpful to me as I don't want to use collation with hql or with SQL Server. I'm open to use collation with Criteria but don't know how to do it.

解决方案

Criteria criteria = s.createCriteria(User.class);
criteria.add(
    Expression.sql("userName = ? collate Latin1_General_CS_AS_KS_WS", userName, new StringType())
);

这篇关于区分大小写在Hibernate Criteria中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆