实际上,我正在返回以下响应(Page-able Response),我想在列表中应用排序,
回复 :
{
"body": {
"totalCount": 500,
"success": true,
"data": [
{
"id": 3361,
"displayName": "Kim",
"events": 57
},
{
"id": 3361,
"displayName": "Sagar",
"events": 57
},
{
"id": 3361,
"displayName": "Jam",
"events": 57
},
{
"id": 4779,
"displayName": "Inga",
"events": 36
},
{
"id": 4779,
"displayName": "Mine",
"events": 36
},
{
"id": 4779,
"displayName": "Joseph",
"events": 36
},
{
"id": 3883,
"displayName": "Amy",
"events": 10
},
{
"id": 14984,
"displayName": "Test20",
"events": 8
},
{
"id": 14984,
"displayName": "Test20",
"events": 8
},
{
"id": 14984,
"displayName": "Test20",
"events": 8
},
{
"id": 14984,
"displayName": "Test20",
"events": 8
},
{
"id": 14984,
"displayName": "Test20",
"events": 8
},
{
"id": 14984,
"displayName": "Test20",
"events": 8
},
{
"id": 14984,
"displayName": "Test20 21",
"events": 8
},
{
"id": 4841,
"displayName": "Patricia",
"events": 7
},
{
"id": 3364,
"displayName": "tutsman",
"events": 4
},
{
"id": 3364,
"displayName": "Jaan",
"events": 4
}
]
},
"statusCode": 200
}
所以问题是,当我请求 API 以及对 displayName 或 id 进行排序时,它起作用了,但在事件的情况下它不起作用。
PS:我知道它不起作用的原因是因为事件显示了事件的大小(已经是一个实体),所以我的实体中没有任何大小列。我刚刚加入表格并获取总事件,然后显示,响应中事件的大小,以及 Pageable 对响应进行排序的基本功能必须是列到实体中,但在我的情况下,我只是在计算规模。
尝试解决方案:
我尝试使用 stream() 函数对 FacadeImpl 中的响应进行排序,它仅适用于特定页面,但是当我再次请求另一个页面排序失败时。
PS:所以我必须在数据库级别对元素进行排序,但是在数据库中我的表中没有任何大小列
请求网址:
localhost:8080/users/3?page=0&size=20&sort=events,desc
代码 :
@Entity
@Access(AccessType.FIELD)
public class Users Serializable {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private List<Event> events;
@Column(name = "first_name")
private String firstName;
//below are the getter and the setters
}
在服务类中:
我正在使用 JPARepo insted of crud 并使用内置方法(findAll)
@Override
@Transactional(readOnly = true)
public Page<Users> find(Filter filter,Pageable pageable) {
var clientSpecification = specificationGenerator.getClientSpecification(filter);
return clientDao.findAll(clientSpecification, pageable);
}