本文介绍了Criteria API - 在 ElementCollection 中使用 UPPER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有课

@Entity
public class Person{
...
@ElementCollection
private Set<String> tags;
...
}

我想使用 JPA 2.0 Criteria API 来搜索这些标签 - 但不区分大小写.因此,我想将搜索参数设置为 UPPER 并将列设置为 UPPER(伪SQL:select p.* from Person p join Tags t on p.id=t.pId where upper(t.name)=upper('搜索参数'); )

I want to use the JPA 2.0 Criteria API to search over these tags - but in a case insensitive way. Thus I want to both set the search parameter to UPPER and the column to UPPER (Pseudo-SQL: select p.* from Person p join Tags t on p.id=t.pId where upper(t.name)=upper('searchParameter'); )

这是我在标签上没有 UPPER 的代码:

Here is my code without the UPPER on the tags:

  CriteriaBuilder builder = this.em.getCriteriaBuilder();
  CriteriaQuery<Person> query = builder.createQuery(Person.class);
  Root<Person> root = query.from(Person.class);

  return this.em.createQuery(query.select(root).where(
     builder.isMember(builder.upper(builder.literal(searchTag)),
        root.get(Person_.tags)))).getResultList();

其中 searchTag 是输入参数.

where searchTag is the input parameter.

如何将 UPPER 分配给 Person_.tags列"?

How can I assign the UPPER to the Person_.tags "Column"?

换句话说,我想用 Criteria API 编写这个查询:

In other words I want to write this Query with Criteria API:

SELECT p FROM Person p JOIN p.tags t WHERE UPPER(t) = UPPER('searchString')

谢谢

推荐答案

好的,我终于有了解决方案:

Ok, I finally have the solution:

  cQuery.where(
     builder.equal(
        builder.upper(cQuery.from(Relation.class).join(Relation_.aliase)
           .as(String.class)),
        builder.upper(builder.literal(alias))
        )
     );

必须使用.as(..)"方法.

One has to use the ".as(..)" method.

这篇关于Criteria API - 在 ElementCollection 中使用 UPPER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 01:32