简体   繁体   English

如何使用复杂类型的javax.persistence.criteria.Predicate?

[英]How to use javax.persistence.criteria.Predicate with complex types?

I have a method that overrides the method "toPredicate": 我有一个覆盖方法“toPredicate”的方法:

@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
    List<Predicate> predicates = new ArrayList<>();

I have to build the predicates. 我必须构建谓词。 It's easy with simple types, for example: 简单类型很容易,例如:

@Entity
@Table
public class Person {

    @Column
    private String name;

    @Column
    private String surname;

}

With this simple class I can do: 通过这个简单的课程,我可以做到:

@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
    List<Predicate> predicates = new ArrayList<>();

    if (StringUtils.isNotBlank(surname)) {
        predicates.add(cb.equal(root.get("surname"), surname));
    }

    if (StringUtils.isNotBlank(name)) {
        predicates.add(cb.equal(root.get("name"), name));
    }

But if the attribute is a complex type, how can I find the simple attribute contained in the complex type? 但是如果属性是复杂类型,我如何找到复杂类型中包含的简单属性?

This is a potential situation: 这是一种潜在的情况:

@Entity
@Table
public class Person {

    @Column
    private String name;

    @Column
    private String surname;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Document> documents;

}

@Entity
@Table
public class Document {

    @Column
    private String type;

    @Column
    private String code;

}

What I have to do if I want to make a predicate with the complex attribute "documents"? 如果我想用复杂属性“文档”创建谓词,我该怎么办?

I tried: 我试过了:

@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
    List<Predicate> predicates = new ArrayList<>();

    if (StringUtils.isNotBlank(type)) {
        predicates.add(cb.equal(root.get("documents").get("type"), type));
    }

But I have this Exception: 但我有这个例外:

java.lang.IllegalStateException: Illegal attempt to dereference path source [null.documents] of basic type
    at org.hibernate.jpa.criteria.path.AbstractPathImpl.illegalDereference(AbstractPathImpl.java:98)
    at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:191)

My task is to find every Person with a determinate kind of document. 我的任务是找到每个人都有一个确定的文件。 How can I do it? 我该怎么做?

Thanks. 谢谢。

you can use join : 你可以使用join

@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
    Predicate predicate = null;

    if (StringUtils.isNotBlank(type)) {
        predicate = cb.equal(root.join("documents").get("type"), type);

       cq.where(predicate);
       cq.distinct(true);

      }

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

相关问题 如何将 javax.persistence.criteria.* 类添加到 Quarkus 中的 Jandex 索引? - How to add javax.persistence.criteria.* classes to Jandex index in Quarkus? 为什么 javax.persistence.criteria.CriteriaBuilder 是 &#39;Builder&#39;? - Why is javax.persistence.criteria.CriteriaBuilder a 'Builder'? JPA标准API:如何使用IN关键字并查询另一个谓词的结果? - JPA criteria API: How to use IN keyword and query the result for another predicate? 用于OneToMany(DB ManyToMany)关系的Java持久性标准谓词 - Java Persistence Criteria Predicate for OneToMany(DB ManyToMany) Relationship 如何使用Querydsl构造涉及多个表的复杂谓词? - How to use Querydsl to construct complex predicate that involves multiple tables? java.lang.ClassNotFoundException:javax.persistence.criteria.Selection - java.lang.ClassNotFoundException: javax.persistence.criteria.Selection 使用Javax-RS和Java持久性及RESTful发布复杂的JSON - POST a Complex JSON using Javax-RS & Java Persistence & RESTful 如何在axis2中使用复杂类型 - how to use complex types in axis2 如何修复javax.persistence.Table.indexes()[Ljavax / persistence / Index? - How to fix javax.persistence.Table.indexes()[Ljavax/persistence/Index? 如何使用JPA 2.1属性javax.persistence.schema-generation.database.action? - How to use JPA 2.1 property javax.persistence.schema-generation.database.action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM