Spring Boot @Formula displaying same likeCount on every result
Spring Boot @Formula displaying same likeCount on every result
You do the match with every song rows :
INNER JOIN song s on us.song_id = s.id WHERE us.song_id = s.id
What you want is restricting it to the current song entity instance.
In fact, if the @Formula
annotation is specified on a field of the Song
entity, you dont want to specify Song
in the query, instead you want to directly refer the field of the current entity (here id
), that is :
WHERE us.song_id = id
So this should do the job :
@Formula((SELECT COUNT(*) FROM user_acc u INNER JOIN user_likes_song us on u.id = us.user_id + WHERE us.song_id = id ))
And I think that you could also simplify more in this way :
@Formula( (SELECT COUNT(*) FROM user_likes_song us WHERE us.song_id = id) )
because the join with user account doesnt matter.