FetchType.LAZY 在多对多中不起作用

FetchType.LAZY in ManyToMany doesnot work

我有一个问题,fetch = FetchType.LAZY,它根本不起作用。我已经花了很多时间解决这个问题,有人可以帮忙吗?我会非常感激。我有一个与电影 manyTomany 相关的流派和国家。无论我多么努力地尝试初始化 LAZY 下载,它 work.I 不需要电影有 EAGER,也不需要类型和国家有 LAZY。 我希望获得具有其类型和国家/地区的电影,但是 SELECT * FROM movie WHERE id = 1 - 我得到一个无限循环,尽管类型和国家/地区有 LAZY 下载。 示例代码 - 下面

实体: 电影

@Entity
@Getter
@Setter
@ToString(of = {"id", "year", "name"})
@EqualsAndHashCode(of = {"id", "year"})
@NoArgsConstructor
@AllArgsConstructor
public class Movie {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    **********
    
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "movie_genre",
            joinColumns = {
                    @JoinColumn(name = "movie_id")},
            inverseJoinColumns = {
                    @JoinColumn(name = "genre_id")})
    private Set<Genre> genres = new HashSet<>();

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "movie_country",
            joinColumns = {
                    @JoinColumn(name = "movie_id")},
            inverseJoinColumns = {
                    @JoinColumn(name = "country_id")})
    private Set<Country> countries = new HashSet<>();
}

流派

@Entity
@Getter
@Setter
@ToString(exclude = "movies")
@EqualsAndHashCode(exclude = "movies")
@NoArgsConstructor
@AllArgsConstructor
public class Genre {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Size(max = 20)
    private String name;

    @ManyToMany(mappedBy = "genres")
    private Set<Movie> movies = new HashSet<>();
}

国家

@Entity
@Getter
@Setter
@ToString(exclude = "movies")
@EqualsAndHashCode(exclude = "movies")
@NoArgsConstructor
@AllArgsConstructor
public class Country {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Size(max = 20)
    private String name;

    @ManyToMany(mappedBy = "countries")
    private Set<Movie> movies = new HashSet<>();
}

控制器

@RestController
public class TestController {

    @Autowired
    private MovieService movieService;

    @Autowired
    private CountryService countryService;

    @Autowired
    private GenreService genreService;

    @GetMapping("movie")
    public List<Movie> getMovieMovie(){
        return movieService.getAll();
    }

    @GetMapping("movie/add")
    public Movie create(){
        Movie movie = new Movie();
        movie.setName("test");
        movie.setImg("test");
        movie.setTime("test");
        movie.setYear((short) 2332);
        movie.setMovieLink("test");
        movie.getCountries().add(countryService.getCountry(1));
        movie.getGenres().add(genreService.getGenre(1));
        return movieService.create(movie);
    }
}

服务

@Service
public class MovieService {

    @Autowired
    private MovieRepository movieRepository;
    
    public List<Movie> getAll(){
        return movieRepository.findAll();
    }

    @Transactional
    public Movie create(Movie mocie){
        return movieRepository.save(mocie);
    }
}

延迟加载按预期工作,因为它延迟加载所有数据。

您正在寻找的是一种打破双向映射循环的方法。

在那里你可以使用你必须在关系上设置的 @JsonManagedReference@JsonBackReference

另请阅读:https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion