Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Criteria API - Using UPPER in a ElementCollection

Tags:

jpa

criteria

I have a class

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

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

where searchTag is the input parameter.

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

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

Thank you

like image 778
Soccertrash Avatar asked Jan 20 '26 00:01

Soccertrash


1 Answers

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

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

like image 85
Soccertrash Avatar answered Jan 23 '26 21:01

Soccertrash