本文介绍了Spring Data以及如何按不在实体中的列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java Spring的新手,并试图了解它的工作原理.我的目标是对不在实体中的JPQL查询进行排序.

I'm new to Java Spring and trying to understand how it works as I go. My goal here is to apply a sort to a JPQL query that is not in an Entity.

我一直在看的参考文献:

References I've been looking at:

Spring Sort类文档

Spring Sort速成课程

如果我声明以下查询

@Query("SELECT a, b.someColumn FROM TableA a INNER JOIN a.tableB b where a.search like %?1%"
public Page<Object[]> findSomething(String search, Pageable pageable);

这将导致对象列表的形式为

This will result in a list of objects with a form of

Object[0] = TableA entity
Object[1] = b.someColumn value

我一直在阅读Sort类,但是我还没有找到通过b.someColumn进行这种排序的方法.

I've been reading up on the Sort class, but I haven't found a way to make this sort by b.someColumn.

如果我执行以下操作

Sort sort = new Sort(Sort.Direction.DESC, "someColumn");
Page<Object[]> things = mgr.findSomething("junk", sort);

然后JPQL结果

当我希望成为

如何防止其将排序列附加到查询中的实体?有没有办法禁用此功能,并使它按字面意义解释我的排序列?是否有更好的分类技术更适合我的情况?

How do I prevent it from appending the sort column to the Entity in the query? Is there a way to disable this function and make it interpret my sort column literally? Is there a better sorting technique that would be more appropriate to my case?

Sort sort = new Sort(Sort.Direction.DESC, "b.someColumn");

推荐答案

好吧,看来我在原始问题中回答了自己的问题.出于某种原因,我的浏览器未及时更新我的​​最新更改,并且我看不到排序有效.

Well, it looks like I answered my own question in my original question. For some reason my browser wasn't up to date with my recent changes and I didn't see the sort working.

引擎将按字面意义解释排序列.

The engine will interpret sort columns literally.

Sort sort = new Sort(Sort.Direction.DESC, "b.someColumn");

将导致

这正是我想要的.

这篇关于Spring Data以及如何按不在实体中的列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:24