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

问题描述

我上课

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

我想使用JPA 2.0 Criteria API搜索这些标签-但不区分大小写.因此,我想将搜索参数都设置为UPPER,将列设置为UPPER(伪SQL:从Person p的p.*连接标签上p.id = t.pId上选择p.*,其中up​​per(t.name)= upper(' searchParameter');)

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

这是我的代码,标签上没有大写字母:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 13:37