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

后端 未结 9 723
天涯浪人
天涯浪人 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

    Need to add @JsonIgnore in child class to avoid such exception. Be careful not to add this annotation in Parent class

    @Entity  
    @Table(name="Movie")
    public class Movie implements Serializable{
    
    @OneToMany(mappedBy="movie",targetEntity=Clip.class,cascade=CascadeType.ALL,
    fetch=FetchType.EAGER)
    private Set clips = new HashSet();
    
    }
    
    @Entity  
    @Table(name="Clip")
    public class Clip implements Serializable{   
        @ManyToOne
        @JoinColumn(name="movie_id")
        @JsonIgnore
        private Movie movie;
    }
    

提交回复
热议问题