使用 Hibernate 进行不区分大小写的搜索

发布于 2024-07-06 02:55:10 字数 305 浏览 9 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

清晰传感 2024-07-13 02:55:10

对于您描述的简单情况,请查看 Restrictions.ilike(),它执行不区分大小写的搜索。

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

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();
街角迷惘 2024-07-13 02:55:10
Criteria crit = session.createCriteria(Person.class);
crit.add(Restrictions.ilike('town', 'fran', MatchMode.ANYWHERE);
List results = crit.list();
Criteria crit = session.createCriteria(Person.class);
crit.add(Restrictions.ilike('town', 'fran', MatchMode.ANYWHERE);
List results = crit.list();
独孤求败 2024-07-13 02:55:10

如果您使用 Spring 的 HibernateTemplate 与 Hibernate 交互,则可以按以下方式对用户的电子邮件地址进行不区分大小写的搜索:

getHibernateTemplate().find("from User where upper(email)=?", emailAddr.toUpperCase());

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:

getHibernateTemplate().find("from User where upper(email)=?", emailAddr.toUpperCase());
素衣风尘叹 2024-07-13 02:55:10

您也不必输入“%”通配符。 您可以传递 MatchMode (< a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/MatchMode.html" rel="nofollow noreferrer">此处先前版本的文档)告诉搜索如何表现。 选项包括 STARTANYWHEREEXACTEND 匹配。

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, and END matches are the options.

蝶舞 2024-07-13 02:55:10

忽略大小写的常用方法是将数据库值和输入值都转换为大写或小写 - 生成的 sql 会有类似

select f.name from f where TO_UPPER(f.name) like '%FRAN%'

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

select f.name from f where TO_UPPER(f.name) like '%FRAN%'

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

橘寄 2024-07-13 02:55:10

@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);

泪痕残 2024-07-13 02:55:10

这也可以使用 org.hibernate.criterion 包中的标准示例来完成。

public List findLike(Object entity, MatchMode matchMode) {
    Example example = Example.create(entity);
    example.enableLike(matchMode);
    example.ignoreCase();
    return getSession().createCriteria(entity.getClass()).add(
            example).list();
}

我发现这只是实现上述目标有用的另一种方法。

This can also be done using the criterion Example, in the org.hibernate.criterion package.

public List findLike(Object entity, MatchMode matchMode) {
    Example example = Example.create(entity);
    example.enableLike(matchMode);
    example.ignoreCase();
    return getSession().createCriteria(entity.getClass()).add(
            example).list();
}

Just another way that I find useful to accomplish the above.

2024-07-13 02:55:10

由于 Hibernate 5.2 session.createCriteria 已被弃用。 以下是使用 JPA 2 CriteriaBuilder 的解决方案。 它使用 likeupper

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
    Root<Person> root = criteria.from(Person.class);

    Expression<String> upper = builder.upper(root.get("town"));
    criteria.where(builder.like(upper, "%FRAN%"));

    session.createQuery(criteria.select(root)).getResultList();

Since Hibernate 5.2 session.createCriteria is deprecated. Below is solution using JPA 2 CriteriaBuilder. It uses like and upper:

    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
    Root<Person> root = criteria.from(Person.class);

    Expression<String> upper = builder.upper(root.get("town"));
    criteria.where(builder.like(upper, "%FRAN%"));

    session.createQuery(criteria.select(root)).getResultList();
哭泣的笑容 2024-07-13 02:55:10

大多数默认数据库排序规则不区分大小写,但在 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.

黑白记忆 2024-07-13 02:55:10

您可以考虑使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文