-1

实际上,我正在返回以下响应(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);
}
4

1 回答 1

1

一种解决方案是创建聚合数据的数据库视图,并通过@SecondaryTable 将您的实体链接到此。

例如:

    create view a_summary_view as
    select
   a_id as id, 
   count(*) as b_count 
from b -- where a is your User Entity and b is Events entity

接着:

@Entity
@Table
@SecondaryTable(name = "a_summary_view", 
       pkJoinColumns = {@PrimaryKeyJoinColumn(name = "id", referencedColumnName= "id")})
public class Users{

   @Column(table = "a_summary_view")
   private Integer eventCount;    
 }

这样,您将在用户实体中有一个字段进行排序

于 2019-08-08T11:53:33.660 回答