Spring数据规范中的转换org.springframework.dao.InvalidDataAccessApiUsageException:非法尝试取消引用路径源

问题描述 投票:-1回答:1

我有以下实体结构:

                            | ParentObject|
                            +-------------+
                                   ^
                                   |
                           +-------+---------+----------------+
                           |                 |                |
                    +-----------+      +-----------+    +-----------+         
                    |   Son1   |       |  Son2     |    |   Son3    | 
                    +-----------+      +-----------+    +-----------+    

我想要的是让所有具有Son1上不存在的属性的Son2和Son3

到目前为止我做了什么:

class ParentObjectPredicat

public static Specification<ParentObject> inNaturesSon2(List<String> natures) {        
    return (root, query, cb) -> {
        final Root<Son2> son2Root = cb.treat(root, Son2.class);
        return son2Root.get(Constants.NATURE).in(natures);
    }
}

public static Specification<ParentObject> inNaturesSon3(List<String> natures) {        
    return (root, query, cb) -> {
        final Root<Son3> son3Root = cb.treat(root, Son3.class);
        return son3Root.get(Constants.NATURE).in(natures);
    }
}

SpecSon2  son2Spec = ParentObjectPredicat.inNaturesSon2(natures);
SpecSon3  son3Spec = ParentObjectPredicat.inNaturesSon3(natures);

Specification<ParentObject> specifications = Specification.where(son2Spec).and(son3Spec);
Iterable<ParentObject> listOfSons = this.parentObjectRepository.findAll(specifications);

我得到的结果是:

org.springframework.dao.InvalidDataAccessApiUsageException:非法尝试取消引用基本类型的路径源[treat(null as mypackage.Son2).nature];嵌套异常是java.lang.IllegalStateException:非法尝试取消引用路径源[treat(null as mypackage.Son2).nature] of basic type

根据我的理解,treat()用于解析子类。有关如何做到这一点的任何建议?

java spring-data-jpa criteria-api
1个回答
0
投票

我正在为那些可能正在寻找答案的人提供Petri Kainulainen的答案:

“遗憾的是,您无法使用JPA编写查询,因为如果您想要”返回“超类对象,JPA不允许您使用从所有子类中找不到的属性。据我所知,您有两种选择:

您可以调用两个查询来获取Son2和Son3对象,并将查询结果组合在Java代码中。你可以使用SQL。“

© www.soinside.com 2019 - 2024. All rights reserved.