title: Spring Data JPA, hibernate: update all query and SET value by selecting from another table
body: <p>Is the following SQL possible in a JPA query? I tried but the actual SQL hibernate runs doesn't seem right.</p>
  
  <p>This is the SQL i want to write as JPA query;</p>
  
  <pre><code>UPDATE movie m SET average_rating = COALESCE((SELECT AVG(stars) FROM rating r WHERE r.movie_id = m.id), 0);
  </code></pre>
  
  <p>This is the JPA query;</p>
  
  <pre><code>@Query("UPDATE Movie m SET m.averageRating = COALESCE((SELECT AVG(r.stars) FROM Rating r WHERE r.movie = m), 0)")
  </code></pre>
  
  <p>And what hibernate says;</p>
  
  <pre><code>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
          )
  </code></pre>
  
  <p>so there seems to be an additional where being added by hibernate.</p>