简体   繁体   English

JPA规范:选择列表属性包含具有特定值的项目的项目

[英]JPA Specifications: Select items where list attribute contains item with certain value

I am trying to solve the following problem with JPA specifications: 我正在尝试解决JPA规范的以下问题:

Consider the following classes: 考虑以下类别:

public class Language {
    private String name;
    ...
}

public class Person {
    private List<Language> languages;
    ...
}

How do I select all Person s, that speak a language with name x ? 如何选择所有说出x的语言的Person I am looking for a solution using the CriteriaBuilder , which should generate a Predicate that I can 'and' together with other predicates. 我现在用的是寻找一个解决方案CriteriaBuilder ,它应该产生一个Predicate ,我可以“和”连同其他谓词。

Thanks in advance. 提前致谢。

It is actually pretty easy: 实际上很简单:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> query = cb.createQuery(Person.class);
Root<Person> root = query.from(Person.class);
ListJoin<Person, Language> langJoin = root.join(Person_.langs);
query.where(cb.equal(langJoin.get(Language_.name), lang));

So you basically join the languages via corresponding association and then add an equals predicate matching the required attribute of the joined entity with your criteria. 因此,您基本上是通过相应的关联来加入语言,然后添加一个equals所连接实体的所需属性与您的条件匹配的equals谓词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM