gpt4 book ai didi

java - spring-data-jpa 中的 JOIN FETCH 不会获取惰性关联

转载 作者:行者123 更新时间:2023-12-02 01:30:30 25 4
gpt4 key购买 nike

我在获取 spring-data-jpa 中的惰性 @ManyToMany 关联时遇到问题。我有一个电影列表,它与喜爱这部电影的用户以及流派列表有关联:

@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,但它仍然是空的。

我没有主意,所以我将不胜感激任何帮助。

更新:这不可能是用户不在数据库中的问题,因为我有另一个查询,该查询仅提取用户在 Collection 夹用户类型中标记为 Collection 夹的电影。因此,如果用户不存在于数据库中,查询将返回空结果,但尽管 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);

最佳答案

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

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

关于java - spring-data-jpa 中的 JOIN FETCH 不会获取惰性关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56134473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com