-1

Actually, I am returning the below response(Page-able Response) and I want to apply the sort on the list,

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
}

So the question is when I request the API along with sort on the displayName or the id, it worked but in the case of events it doesn't work.

PS: I know the reason why it is not worked because the events, showing the size of events(which are already an entity), so I don't have any size column in my entity. I just joined the table and fetch the total events and after that display, the size of events in the response, and the basic feature of Pageable to sort the response must be the column into the entity, but in my case, I am just computing the size.

Tried Solution:

I tried to sort the response in facadeImpl by using the stream() function and it worked only for the particular page but when I request for another page sorting again fails.

PS: so I have to sort the elements on DB level, but in DB I don't have any size column in my table

Requesting url :

localhost:8080/users/3?page=0&size=20&sort=events,desc

Code :

@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    
}

In Service Class:

i am using the JPARepo insted of crud and use there inbuilt method(findAll)

@Override
@Transactional(readOnly = true)
public Page<Users> find(Filter filter,Pageable pageable) {
    var clientSpecification = specificationGenerator.getClientSpecification(filter);
    return clientDao.findAll(clientSpecification, pageable);
}
Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28

1 Answers1

1

One solution can be to create a database view of aggregate data and link your Entity to this by means of a @SecondaryTable.

For example:

    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

And then:

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

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

This way you would have a field in Users Entity to sort upon

bidisha mukherjee
  • 715
  • 1
  • 10
  • 20