Spring data / QueryDSL multiple group by with preservation of paging

Viewed 47

I am facing this issue:

Lets assume we have 3 entities (spring data/hibernate annotations omitted for simplicity)

Class A {
  long id;
  
  B classB;

  C classC;

  ... other fields omitted for simplicity ...
}

Class B {
  long id;
  
  List<A> childrenA;


  ... other fields omitted for simplicity ...
}

Class C {
  long id;
  
  List<A> childrenA;

  ... other fields omitted for simplicity ...
}

What I want to achieve is build some kind of "grid" where user can see/filter and sort aggregated results by class B or C. The main "decision" is made like this: if A record has B parent, group it by B. If not, group it by C And thats the main problem. I think I got to use QueryDSL instead of spring-data cause there could be dynamic filter and sorting. The other important thing is I want to page the result. So I assume I have to select X A rows and then group them together. Which will break the paging (cause I, for example, select 50 items which is one page and I group them into 10 grouped items, which is not a correct amount). Is this possible to solve by Spring-data-jpa or QueryDSL? To group it like that, return that exact page size of items and return some kind of dto or something? I would really like to avoid using QueryDSL SQL version.

To understand the business case more: Imagine you are selling items. And you want a some kind of grid, where you want to group items which are in orders by this order (so if order B has 10 items A, I have to select Dto holding data about B and list of that 10 A items). And the other records I want to group by warehouse for example. So the rest of the records should be grouped by linked C warehouse (in case they are not linked to any B order)

0 Answers
Related