Is the following SQL possible in a JPA query? I tried but the actual SQL hibernate runs doesn't seem right.
This is the SQL i want to write as JPA query;
UPDATE movie m SET average_rating = COALESCE((SELECT AVG(stars) FROM rating r WHERE r.movie_id = m.id), 0);
This is the JPA query;
@Query("UPDATE Movie m SET m.averageRating = COALESCE((SELECT AVG(r.stars) FROM Rating r WHERE r.movie = m), 0)")
And what hibernate says;
Hibernate:
insert
into
HT_Movie
select
movie0_.id as id
from
Movie movie0_
Hibernate:
update
Movie
set
average_rating=coalesce((select
avg(rating1_.stars)
from
Rating rating1_
where
rating1_.movie_id=id),
0)
where
(
id
) IN (
select
id
from
HT_Movie
)
so there seems to be an additional where being added by hibernate.
@Query("UPDATE Movie m SET m.averageRating = COALESCE((SELECT AVG(r.stars) FROM Rating r WHERE r.movie = m), 0)")
This is 100% correct. And hibernate works correctly as well. Hibernate works in this way:
HT_Movie
and insert the ids of Movie table. HT_Movie
t enforce more speed in update processing And this is not new hibernate always fabricates queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.