spring-data-jpa中的JOIN FETCH无法获取惰性关联

时间:2019-05-14 16:02:33

标签: java hibernate jpa spring-data-jpa spring-data

我在获取@ManyToMany中的惰性spring-data-jpa关联时遇到问题。 我有一个电影列表,该电影列表与使该电影成为最受欢迎的用户和类型列表相关联:

@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<>;
}

类型也与已将该类型标记为收藏的用户相关联:

@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;
}

我有一个存储库,其中包含以下方法/查询,该存储库必须按喜欢的流派返回所有电影,并加载电影的用户,这样我才能显示用户是否将该电影标记为喜欢的电影:

@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);

但是这里我遇到了一个问题,movie.getUsers()在执行此查询后为空。

found可以使用@EntityGraph来提供帮助,但并不是movie.getUsers()关联仍然为空。

我也试图为了进行测试而将此关联设置为FetchType.EAGER,但它仍然是空的。

我没有主意,所以我将不胜感激。

UPD: 用户不在数据库中可能不是问题,因为我有另一个查询,该查询仅提取在收藏夹用户类型中被用户标记为收藏夹的电影。因此,如果用户不存在于数据库中,则查询将返回空结果,但尽管movie.getUsers()为空,但我确实得到了结果。这是方法/查询:

@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);

1 个答案:

答案 0 :(得分:2)

是的,单向映射应该起作用。对于您正在做的基本示例,我没有任何问题。这应该是一个很好的起点,可以帮助您找出差异。我注意到我没有加入movie.users中的countQuery

@Entity
public class Movie {
    @Id @GeneratedValue private Long id;
    @ManyToMany
    private Set<Genre> genres;
    @ManyToMany
    private Set<User> users;

@Entity
public class Genre {
    @Id @GeneratedValue private Long id;        
    @ManyToMany
    private Set<User> users;

@Entity
public class User {
    @Id @GeneratedValue private Long id;

还有存储库:

public interface MovieRepository extends JpaRepository<Movie, Long> {
    @Query(value = "SELECT movie FROM Movie movie " +
            "JOIN movie.genres genres JOIN genres.users users LEFT JOIN FETCH movie.users " +
            "WHERE users.id = :userId", 
    countQuery = "SELECT COUNT(movie) FROM Movie movie " +
            "JOIN movie.genres genres JOIN genres.users users " +
            "WHERE users.id = :userId")
    Page<Movie> getAllMoviesByFavoriteGenres(@Param("userId") Long userId, Pageable page);

并使用它:

@Override
public void run(String... args) throws Exception {
    create();
    System.out.println("something");
    Page<Movie> movies = movieRepo.getAllMoviesByFavoriteGenres(1L, PageRequest.of(0, 10));
    movies.forEach(System.out::println);
}

private void create() {
    User u1 = new User();
    userRepo.save(u1);

    Set<User> users = Collections.singleton(u1);

    Genre g1 = new Genre();
    g1.setUsers(users);
    genreRepo.save(g1);

    Set<Genre> genres = Collections.singleton(g1);

    Movie m1 = new Movie();
    m1.setGenres(genres);
    m1.setUsers(users);
    movieRepo.save(m1);
}