I'm having a problem with fetching lazy @ManyToMany
association in spring-data-jpa
.
I have a list of movies, that has an association with users that has made this movie favorite and the list of genres:
@Entity
@Table("movie")
public class Movie {
...
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(...)
private List<Genre> genres;
@ManyToMany
@JoinTable(name = "users_movies",
joinColumns = @JoinColumn(name = "movie_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private List<User> users = new ArrayList<>;
}
Genres also have an association with users that have marked this genre as a favorite:
@Entity
@Table("genre")
public class Genre {
....
@ManyToMany
@JoinTable(name = "users_genres",
joinColumns = @JoinColumn(name = "genre_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private List<User> users;
}
And I have a repository with the following method/query that has to return all the movies by the favorite genres and to load the user of the movies so I can display if the user has marked the movie as a favorite:
@Query(value = "SELECT movie FROM Movie movie " +
"JOIN movie.genres genre JOIN genre.users grenreUser " +
"LEFT JOIN FETCH movie.users " +
"WHERE grenreUser.id = :userId",
countQuery = "SELECT COUNT(movie) FROM Movie movie " +
"JOIN movie.genres genre JOIN genre.users grenreUser " +
"LEFT JOIN movie.users " +
"WHERE grenreUser.id = :userId")
Page<Movie> getAllMoviesByFavoriteGenres(@Param("userId") String userId, Pageable);
But here I'm having a problem, movie.getUsers()
is empty after executing this query.
I found that using @EntityGraph
could help, but it didn't the movie.getUsers()
association is still empty.
I also have tried to make this association as FetchType.EAGER
just for the sake of testing, but it still is empty.
I'm out of ideas so I will appreciate any help.
UPD:
It could not be the issue with users not be present in DB, as I have another query that extracts only the movies that are marked favorites by the user in the favorites user genre. So if the user would have not been present in DB, the query would return me the empty result, but I do get result although the movie.getUsers()
is empty. Here is the method/query:
@Query(value = "SELECT movie FROM Movie movie " +
"JOIN movie.genres genre " +
"JOIN FETCH movie.users user " +
"WHERE user.id = :userId AND :userId MEMBER OF genre.users",
countQuery = "*same but with count*")
Page<Movie> getAllFavoriteMoviesByFavoriteGenres(@Param("userId") String userId, Pageable);