我有一個實體的彈簧引導項目,在具體的,我有一個學生類與DesiredCourses列表,應該是一個集<>。JsonMappingException當使用集而不是列表
當我使用:
@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])
有我丟失的東西或一些額外的需要做完了?
按照要求,equals和哈希碼
@Override
public 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;
}
@Override
public int hashCode() {
int result = priority.hashCode();
result = 31 * result + course.hashCode();
result = 31 * result + student.hashCode();
return result;
}
我想知道如果哈希碼還是equals方法可能是罪魁禍首你是否擁有StudentDesiredCourseEntity的HashCode和Equals? – alexwen
發表了等號和哈希碼 –
Ahhhh你知道什麼,我從來沒有考慮過我的學生或課程可能會返回爲空....我敢打賭,這是問題 –