使用 Hibernate 进行不区分大小写的搜索
我正在使用 Hibernate 将 Java 应用程序的 ORM 转换为 Oracle 数据库(数据库供应商并不重要,有一天我们可能会切换到另一个数据库),并且我想根据用户提供的字符串从数据库中检索对象。 例如,在搜索人员时,如果用户正在寻找住在“fran”的人,我希望能够为她提供旧金山的人。
SQL 不是我的强项,而且我更喜欢 Hibernate 的 Criteria 构建代码,而不是硬编码的字符串。 任何人都可以为我指明如何在代码中执行此操作的正确方向,如果不可能,硬编码的 SQL 应该是什么样子?
谢谢,
尤瓦尔=8-)
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-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于您描述的简单情况,请查看 Restrictions.ilike(),它执行不区分大小写的搜索。
For the simple case you describe, look at Restrictions.ilike(), which does a case-insensitive search.
如果您使用 Spring 的 HibernateTemplate 与 Hibernate 交互,则可以按以下方式对用户的电子邮件地址进行不区分大小写的搜索:
If you use Spring's HibernateTemplate to interact with Hibernate, here is how you would do a case insensitive search on a user's email address:
您也不必输入“%”通配符。 您可以传递 MatchMode (< a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/MatchMode.html" rel="nofollow noreferrer">此处先前版本的文档)告诉搜索如何表现。 选项包括
START
、ANYWHERE
、EXACT
和END
匹配。You also do not have to put in the '%' wildcards. You can pass MatchMode (docs for previous releases here) in to tell the search how to behave.
START
,ANYWHERE
,EXACT
, andEND
matches are the options.忽略大小写的常用方法是将数据库值和输入值都转换为大写或小写 - 生成的 sql 会有类似
In hibernate criteria requests.like(...).ignoreCase()
我更熟悉使用 Nhibernate,因此语法可能不是 100% 准确,
有关更多信息,请参阅 Pro Hibernate 3 提取 和 hibernate 文档 15.2。 缩小结果集
The usual approach to ignoring case is to convert both the database values and the input value to upper or lower case - the resultant sql would have something like
In hibernate criteria restrictions.like(...).ignoreCase()
I'm more familiar with Nhibernate so the syntax might not be 100% accurate
for some more info see pro hibernate 3 extract and hibernate docs 15.2. Narrowing the result set
@Query(value = "来自用户 u WHERE UPPER(u.email) LIKE UPPER(?1)")
列表 findByEmailIgnoreCase(final String email);
@Query(value = "FROM User u WHERE UPPER(u.email) LIKE UPPER(?1)")
List findByEmailIgnoreCase(final String email);
这也可以使用 org.hibernate.criterion 包中的标准示例来完成。
我发现这只是实现上述目标有用的另一种方法。
This can also be done using the criterion Example, in the org.hibernate.criterion package.
Just another way that I find useful to accomplish the above.
由于 Hibernate 5.2
session.createCriteria
已被弃用。 以下是使用 JPA 2 CriteriaBuilder 的解决方案。 它使用like
和upper
:Since Hibernate 5.2
session.createCriteria
is deprecated. Below is solution using JPA 2 CriteriaBuilder. It useslike
andupper
:大多数默认数据库排序规则不区分大小写,但在 SQL Server 世界中,可以在实例、数据库和列级别设置它。
Most default database collations are not case-sensitive, but in the SQL Server world it can be set at the instance, the database, and the column level.
您可以考虑使用 Compass 作为 lucene 之上的包装器。
http://www.compass-project.org/
通过向域对象添加一些注释你会取得这样的成就。
Compass 提供了一个简单的 API 来与 Lucene 配合使用。 如果您知道如何使用 ORM,那么您会对 Compass 感到很熟悉,只需进行简单的保存、删除和删除操作即可。 询问。
来自网站本身。
“Compass 构建在 Lucene 之上,简化了 Lucene 的常见使用模式,例如 google 式搜索、索引更新以及缓存和索引分片(子索引)等更高级的概念。Compass 还使用内置优化来实现并发提交和合并。”
我过去用过这个,我发现它很棒。
You could look at using Compass a wrapper above lucene.
http://www.compass-project.org/
By adding a few annotations to your domain objects you get achieve this kind of thing.
Compass provides a simple API for working with Lucene. If you know how to use an ORM, then you will feel right at home with Compass with simple operations for save, and delete & query.
From the site itself.
"Building on top of Lucene, Compass simplifies common usage patterns of Lucene such as google-style search, index updates as well as more advanced concepts such as caching and index sharding (sub indexes). Compass also uses built in optimizations for concurrent commits and merges."
I have used this in the past and I find it great.