Calculate and save a pre-calculated average using Hibernate

Say I have a set of products, and each product has many reviews. Each review has a rating. I will need an average expense much more often than then I will need actual reviews. Are there any best practices / clean ways to pre-calculate the average value and store it in sleep mode.

I am currently thinking of adding a review counter and looking at the sum columns in the product table and equipping the add verification method to update the invoice and amount.

I would be very happy to hear about a cleaner way of doing this.

+5
source share
3

. , ( ...).

, , , db ( psuedo SQL/HQL):

1) :

select sum(score)/count from reviews where product_id = ?

2) .

select product_name, sum(r.score)/count(r.score)
from products p
join reviews r
group by product_name

, - product_id .

, HQL, . , .

show_sql TestTestTest, , hibernate / .

+2

, , , , .

- , /, ( ) .

0

Request caching in sleep mode can be a way. Otherwise, go with your original idea.

0
source

All Articles