使用Hibernate进行不区分大小写的搜索 [英] Case-insensitive search using Hibernate

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

问题描述

我使用Hibernate将Java应用程序的ORM应用到Oracle数据库(不是数据库供应商很重要,我们可能有一天会切换到另一个数据库),并且我想根据用户提供的数据库检索对象字符串。例如,在搜索人物时,如果用户正在寻找居住在fran的人,我希望能够在旧金山给她的人员。

SQL不是我的强项,我更喜欢Hibernate的 Criteria 构建代码来硬编码字符串。任何人都可以在正确的方向上指出我如何在代码中实现这一点,如果不可能,硬编码的SQL应该是什么样子?



谢谢,



Yuval = 8 - )

解决方案

在Restrictions.ilike(),它不区分大小写的搜索。

pre $ Criteria crit = session.createCriteria(Person.class) ;
crit.add(Restrictions.ilike('town','%fran%');
列出结果= crit.list();


I'm using Hibernate for ORM of my Java app to an Oracle database (not that the database vendor matters, we may switch to another database one day), and I want to retrieve objects from the database according to user-provided strings. For example, when searching for people, if the user is looking for people who live in 'fran', I want to be able to give her people in San Francisco.

SQL is not my strong suit, and I prefer Hibernate's Criteria building code to hard-coded strings as it is. Can anyone point me in the right direction about how to do this in code, and if impossible, how the hard-coded SQL should look like?

Thanks,

Yuval =8-)

解决方案

For the simple case you describe, look at Restrictions.ilike(), which does a case-insensitive search.

Criteria crit = session.createCriteria(Person.class);
crit.add(Restrictions.ilike('town', '%fran%');
List results = crit.list();

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

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