2017-05-22 69 views
0

我已經定義了2個類,關係似乎對我很好,但是當我嘗試序列化父對象時,序列化程序進入無限循環。這裏是類序列化OneToMany關係導致無限循環

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

所以當我嘗試serialie我的項目類,串行器進入無限循環。我使用Spring引導與hibernate4Module

+0

我找到了答案,不得不添加@JsonIgnore在多對一側。 – allthenutsandbolts

回答