序列化OneToMany关系会导致无限循环

时间:2017-05-22 14:50:31

标签: hibernate-mapping

我已经定义了2个类,关系对我来说似乎很好,但是当我尝试序列化父对象时,序列化器进入无限循环。这是课程

  @Entity
@Table(name = "lociprojects")
public class Projects implements Serializable {

    public Projects() {

    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="PROJ_ID")
    Long id;

    @Column(nullable = false)
    private String projectTitle;
    @Column(nullable = false)
    private String userName;

    private String projectType;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Collection<Search> search;
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Collection<SearchResults> searchResults;
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL,mappedBy ="project")
    private Set<CompoundResults> compoundResults;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setSearchResults(Collection<SearchResults> searchResults) {
        this.searchResults = searchResults;
    }

    public Collection<Search> getSearch() {
        return search;
    }

    public void setSearch(Collection<Search> search) {
        this.search = search;
    }


    public Collection<SearchResults> getSearchResults() {
        return searchResults;
    }

    public void setSearchresults(Collection<SearchResults> searchResults) {
        this.searchResults = searchResults;
    }

    public Collection<CompoundResults> getCompoundResults() {
        return compoundResults;
    }

    public void setCompoundResults(Set<CompoundResults> compoundResults) {
        this.compoundResults = compoundResults;
    }

    public String getProjectTitle() {
        return projectTitle;
    }

    public void setProjectTitle(String projectTitle) {
        this.projectTitle = projectTitle;
    }

    // This class is for InnerClass

    public class CompKey implements Serializable {

        public CompKey() {

        }

        private String projectTitle;
        private String userName;

    }

    public String getProjectType() {
        return projectType;
    }

    public void setProjectType(String projectType) {
        this.projectType = projectType;
    }

}




@Entity
@Table(name = "compoundResults")
public class CompoundResults {

    public CompoundResults() {
    }

    @Id
    @GeneratedValue
    @Column(name = "COMPR_ID")
    private Long id;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Collection<SearchCompound> points;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PROJ_ID")
    private Projects project;


    String title;

    String type;


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Collection<SearchCompound> getPoints() {
        return points;
    }

    public void setPoints(Collection<SearchCompound> points) {
        this.points = points;
    }

    public Projects getProject() {
        return project;
    }

    public void setProject(Projects project) {
        this.project = project;
    }

因此,当我尝试序列化我的项目类时,序列化器进入无限循环。我正在使用Spring Boot和hibernate4Module

1 个答案:

答案 0 :(得分:1)

必须在ManytoOne Side添加@JsonIgnore