我想执行此方法:
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Project> listProjects() {
return ps.findAllProjects();
}
在我的 DAO 中,我有:
@Repository
public interface ProjectDAO extends JpaRepository<Project, Long> {
List<Project> findAllProjectsByShowableTrue(); }
在我的实体中:
1。项目:
@Transactional
@Entity
public class Project implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
private boolean showable = true;
@OneToMany(mappedBy="project", fetch = FetchType.EAGER)
private Set<Collaborator> collaborators = new HashSet<>();
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((collaborators == null) ? 0 : collaborators.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
}
//Getters & setters
}
2。合作者:
@Entity
public class Collaborator implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "ProjectID" , nullable = true)
private Project project;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((project == null) ? 0 : project.hashCode());
return result;
}
//Getters & setters
}
但是我得到了这个错误:
HTTP Status 500 - Handler dispatch failed; nested exception is java.lang.StackOverflowError
root cause
java.lang.StackOverflowError
java.net.AbstractPlainSocketImpl.available(Unknown Source)
java.net.SocketInputStream.available(Unknown Source)
.........
gestionprojet.java.entities.beans.Project.hashCode(Project.java:179)
gestionprojet.java.entities.beans.Collaborator.hashCode(Collaborator.java:88)
现在,当协作者表为空时:请求运行完美,但是当我向项目添加协作者时,所有相关请求都会被破坏。
P.S: HashCode()&amp; Equals()方法是自动生成的。
任何帮助将不胜感激。坦克你。
答案 0 :(得分:0)
您的Project
班级正在呼叫collaborators.hashCode()
,而您的Collaborators
班级正在呼叫project.hashCode()
。
因此,当一个项目有一个协作者时,你会得到一个无限循环,因为Project
调用Collaborator
,Collaborator
调用Project
等等......