I tried to return the average value and the number of sets of ratings in one query. I dealt with this quite easily in two queries, following the example I found in the browser. For instance:
@Query("SELECT AVG(rating) from UserVideoRating where videoId=:videoId") public double findAverageByVideoId(@Param("videoId") long videoId);
but as soon as I wanted to average and count in the same query, the problem started. After many hours of experimentation, I found that it worked, so I share it here. Hope this helps.
1) I need a new class for the results:
I had to refer to this class in the request:
@Query("SELECT new org.magnum.mobilecloud.video.model.AggregateResults(AVG(rating) as rating, COUNT(rating) as TotalRatings) from UserVideoRating where videoId=:videoId") public AggregateResults findAvgRatingByVideoId(@Param("videoId") long videoId);
One query now returns the average rating and the number of ratings
source share