【问题标题】:Jpa Specification ORA-01791: not a SELECTed expressionJpa 规范 ORA-01791: 不是 SELECTed 表达式
【发布时间】:2020-12-15 16:19:30
【问题描述】:

我对 Jpa 规范有疑问。
我的规范如下所示:

public static Specification<PersonView> getByFilter(PersonViewFilter filter) {
    return (root, query, criteriaBuilder) -> {

        List<Predicate> predicates = new ArrayList<>();

        SetJoin<PersonView, PersonDetailsView> personDetailsJoin =
                root.join(PersonView_.personDetails);

        Path<String> namePath = personDetailsJoin.get(PersonDetailsView_.name);
        Path<String> surnamePath = personDetailsJoin.get(PersonDetailsView_.surname);

        predicates.add(namePredicate(personDetailsJoin, criteriaBuilder, filter.getName()));
        predicates.add(surnamePredicate(personDetailsJoin, criteriaBuilder, filter.getSurname()));
        predicates.add(peselPredicate(personDetailsJoin, criteriaBuilder, filter.getPesel()));

        predicates.removeAll(Collections.singleton(EMPTY_PREDICATE));

        query.orderBy(List.of(criteriaBuilder.asc(namePath), criteriaBuilder.asc(surnamePath))).distinct(true);
        return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
    };
}

当我尝试搜索结果时,我会看到一个错误: ORA-01791:不是 SELECTed 表达式

问题与 JpaSpecificationExecutor 生成的 sql 有关。 它看起来像这样:

select
    * 
from
    ( select distinct pv.person_id 
    from
        personview pv
    inner join
        persondetailsview pdv on pv.person_id=pdv.person_id 
    where
        1=1 
    order by
        pdv.name asc,
        pdv.surname asc ) 
where
    rownum <= ?

在 Select clausule 中应该添加姓名和姓氏然后它会起作用,但我不知道如何在规范中执行此操作。没有不同的查询工作,但我没有重复。 请寻求帮助。

【问题讨论】:

    标签: sql spring oracle jpa repository


    【解决方案1】:

    从错误 ORA-01791: not a SELECTed expression 中,您想将字段添加到选择中或使用以下内容删除不同的字段:

    query.distinct(false);
    

    如果你想保持独特,看看那里: https://stackoverflow.com/a/53549880/2641426

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 2016-01-24
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      相关资源
      最近更新 更多