-2

For example I need show statistic table on web page. This table consists of 5 rows:

AVG-rating | 5
TotalSum   | 12.1
SumToday   | 2.1
SummMonth  | 8.6
SomeElse   | 666 

Each value in this table - calculated using an aggregate function in a database.

Question: How is more correct to get this data with spring-data-jpa?

Now I have One service - StatisticService with public methos getStatistic(user);

In this method I call 5 methods from repository for each statistic value and form response. Each repository method - native query.

I do not like this aproach. And I think best way is create View in database and select all statistic in one query to view.

But this aproach I do not like too. Because I'm tied to the base. and the base contains logic. Although in the first case I use native queries, but I can rewrite them to JPQL (but maybe not all).

How to more correctly extract aggregated information from the database with spring-data-jpa?

ip696
  • 6,574
  • 12
  • 65
  • 128
  • I would create a view and map it to the Entity using `@SecondaryTable` and then they just become normal properties of the entity. The big advantage of this approach is that you can sort and filter at the database level using standard repository methods. https://stackoverflow.com/questions/33778895/best-way-to-get-aggregate-function-result-inside-the-entity-object/33782331#33782331 – Alan Hay Dec 05 '18 at 13:42

1 Answers1

0

Since you have all the values calculated inside DB I do not see any opportunities rather than you call every 5 queries. Otherwise you need to implement this calculation logic inside your code

Nick
  • 744
  • 6
  • 11