Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 复制到db时,选择distinct with order by runs OK,但从应用程序运行时失败_Hibernate_Sql Order By_Distinct_Criteria Api - Fatal编程技术网

Hibernate 复制到db时,选择distinct with order by runs OK,但从应用程序运行时失败

Hibernate 复制到db时,选择distinct with order by runs OK,但从应用程序运行时失败,hibernate,sql-order-by,distinct,criteria-api,Hibernate,Sql Order By,Distinct,Criteria Api,我有一个查询,当我从应用程序控制台复制它时运行正常,但由于应用程序中的错误而失败: 原因:org.postgresql.util.PSQLException:错误:对于SELECT DISTINCT,ORDER by表达式必须出现在SELECT列表中 但当我查看查询时,我看到我排序依据的列在选择列表中 按规范创建查询的方法(请注意ordery by和distinct) private CriteriaQuery createSearchQuery(规范规范,字符串搜索查询){ CriteriaB

我有一个查询,当我从应用程序控制台复制它时运行正常,但由于应用程序中的错误而失败: 原因:org.postgresql.util.PSQLException:错误:对于SELECT DISTINCT,ORDER by表达式必须出现在SELECT列表中

但当我查看查询时,我看到我排序依据的列在选择列表中

按规范创建查询的方法(请注意ordery by和distinct)

private CriteriaQuery createSearchQuery(规范规范,字符串搜索查询){
CriteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery queryForNode=builder.createTupleQuery();
Root nodeRoot=queryForNode.from(Node.class);
Join-Join=nodeRoot.Join(节点\文档);
表达式orderyBy=builder.function(
"军衔",,
双层,
builder.function(“to_tsvector”,Object.class,join.get(FTSDocument_.document)),
builder.function(“plainto_tsquery”、Object.class、builder.literal(searchQuery))
);
queryForNode
.multiselect(nodeRoot,orderyBy)
.distinct(正确)
.where(规范toPredicate(节点、查询角节点、建筑商))
.orderBy(builder.desc(orderyBy));
返回queryForNode;
}
主要方法:

    public Page<Node> findDistinctBySpec(Specification<Node> spec, String searchQuery, Pageable pageable) {

        // Get page content
        List<Tuple> resultList = entityManager.createQuery(createSearchQuery(spec, searchQuery))
            .setFirstResult(pageable.getPageNumber() * pageable.getPageSize())
            .setMaxResults(pageable.getPageSize())
            .getResultList();

        List<Node> nodes = resultList.stream().map(tuple-> tuple.get(0,Node.class)).collect(Collectors.toList());

        // Get total count of matched results
        TypedQuery<Long> typedQueryForCount = entityManager.createQuery(createCountQuery(spec));
        Long totalCount = typedQueryForCount.getSingleResult();

        return new PageImpl<>(nodes, pageable, totalCount);
    }
以下是postresql数据库日志:

2019-06-04 13:23:16.282 EEST [6820] LOG:  execute <unnamed>: BEGIN
select distinct node0_."id" as col_0_0_, ts_rank(to_tsvector(ftsdocumen1_."document"), plainto_tsquery($1)) as col_1_0_, node0_."id" as id1_7_, node0_."created_by" as created_2_7_, node0_."created_date" as created_3_7_, node0_."last_modified_by" as last_mod4_7_, node0_."last_modified_date" as last_mod5_7_, node0_."alias" as alias6_7_, node0_."description" as descript7_7_, node0_."image_url" as image_ur8_7_, node0_."left" as left9_7_, node0_."name" as name10_7_, node0_."right" as right11_7_, node0_."space_id" as space_i14_7_, node0_."state" as state12_7_, node0_."type" as type13_7_, node0_."user_group_id" as user_gr15_7_ from "node" node0_ inner join "node_document" ftsdocumen1_ on node0_."id"=ftsdocumen1_."id" left outer join "user_group" usergroup2_ on node0_."user_group_id"=usergroup2_."id" left outer join "user_user_group" users3_ on usergroup2_."id"=users3_."user_group_id" left outer join "jhi_user" user4_ on users3_."user_id"=user4_."id" inner join "space" space5_ on node0_."space_id"=space5_."id" where space5_."state"=0 and node0_."state"=0 and (node0_."user_group_id" is null or user4_."login"=$2) and 1=1 and to_tsvector('english', document ) @@ plainto_tsquery( $3 )=true order by ts_rank(to_tsvector(ftsdocumen1_."document"), plainto_tsquery($4)) desc limit $5 node0_."id" as col_0_0_, ts_rank(to_tsvector(ftsdocumen1_."document"), plainto_tsquery($1)) as col_1_0_, node0_."id" as id1_7_, node0_."created_by" as created_2_7_, node0_."created_date" as created_3_7_, node0_."last_modified_by" as last_mod4_7_, node0_."last_modified_date" as last_mod5_7_, node0_."alias" as alias6_7_, node0_."description" as descript7_7_, node0_."image_url" as image_ur8_7_, node0_."left" as left9_7_, node0_."name" as name10_7_, node0_."right" as right11_7_, node0_."space_id" as space_i14_7_, node0_."state" as state12_7_, node0_."type" as type13_7_, node0_."user_group_id" as user_gr15_7_ from "node" node0_ inner join "node_document" ftsdocumen1_ on node0_."id"=ftsdocumen1_."id" left outer join "user_group" usergroup2_ on node0_."user_group_id"=usergroup2_."id" left outer join "user_user_group" users3_ on usergroup2_."id"=users3_."user_group_id" left outer join "jhi_user" user4_ on users3_."user_id"=user4_."id" inner join "space" space5_ on node0_."space_id"=space5_."id" where space5_."state"=0 and node0_."state"=0 and (node0_."user_group_id" is null or user4_."login"=$2) and 1=1 and to_tsvector('english', document ) @@ plainto_tsquery( $3 )=true order by ts_rank(to_tsvector(ftsdocumen1_."document"), plainto_tsquery($4)) desc limit $5
2019-06-04 13:23:16.323 EEST [6820] LOG:  execute S_3: ROLLBACK
人们说,当order by with参数不起作用时,这是一个Hibernate错误,应该在版本6中修复

同时,对我有效的解决方案是按选择列表中的列编号排序,如下所示:

.orderBy(builder.desc(builder.literal(2));
人们说,当order by with参数不起作用时,这是一个休眠错误,应该在版本6中修复

同时,对我有效的解决方案是按选择列表中的列编号排序,如下所示:

.orderBy(builder.desc(builder.literal(2));