Hibernate @OneToMany Relationship Causes Infinite Loop Or Empty Entries in JSON Result

后端 未结 9 738
天涯浪人
天涯浪人 2020-12-06 05:07

I have two entities, an entity \"movie\" and an entity \"Clip\" each clip belongs to one movie and a movie can have multiple clips.

My code looks like:



        
9条回答
  •  忘掉有多难
    2020-12-06 05:23

    Solution:

    Use

    @JsonManagedReference annotation for the first objects instantiated

    @JsonBackReference annotation for the second objects instantiated

    Movie.java

    @JsonManagedReference
    @OneToMany(mappedBy = "movie", targetEntity = Clip.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
        private Set clips = new HashSet();
    

    Clip.java

    @JsonBackReference
    @ManyToOne
        @JoinColumn(name="movie_id")
        private Movie movie;
    

提交回复
热议问题