我有一个带有某些实体的spring boot项目,具体来说,我有一个带有DesiredCourses列表的Student Class,该列表应该是Set 。当我使用时:@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)public List<StudentDesiredCourseEntity> getStudentDesiredCourses() { return studentDesiredCourses;}public void setStudentDesiredCourses(List<StudentDesiredCourseEntity> studentDesiredCourses) { this.studentDesiredCourses = studentDesiredCourses;}一切正常,但是当我使用@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)public Set<StudentDesiredCourseEntity> getStudentDesiredCourses() { return studentDesiredCourses;}public void setStudentDesiredCourses(Set<StudentDesiredCourseEntity> studentDesiredCourses) { this.studentDesiredCourses = studentDesiredCourses;}我懂了org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: (was java.lang.NullPointerException) (through reference chain: edu.cs6310.project4.entities.StudentEntity[\"studentDesiredCourses\"]->java.util.HashSet[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: edu.cs6310.project4.entities.StudentEntity[\"studentDesiredCourses\"]->java.util.HashSet[0])是否有我想念的东西或需要做的其他事情?根据要求,等于和哈希码@Overridepublic boolean equals(Object o) { if (this == o) return true; if (!(o instanceof StudentDesiredCourseEntity)) return false; StudentDesiredCourseEntity that = (StudentDesiredCourseEntity) o; if (!course.equals(that.course)) return false; if (!priority.equals(that.priority)) return false; if (!student.equals(that.student)) return false; return true;}@Overridepublic int hashCode() { int result = priority.hashCode(); result = 31 * result + course.hashCode(); result = 31 * result + student.hashCode(); return result;} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 正如alexwen在评论中提到的,之所以不起作用,是因为未在hashcode/equals方法中处理null (adsbygoogle = window.adsbygoogle || []).push({});
10-07 11:59