У меня есть проект начальной загрузки с некоторыми объектами, в частности, у меня есть класс учеников с списком желаемых вызовов, который должен быть набором <>.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])
Есть ли что-то мне не хватает или что-то дополнительное, что нужно сделать?
В соответствии с просьбой, равенства и хэш-код
@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;
}
Я интересно, если HashCode или Equals метод может быть у вас есть HashCode и Equals для StudentDesiredCourseEntity? – alexwen
Написал equals и хеш-код –
Ahhhh вы знаете, что, я никогда не считал, что мой ученик или курс могут вернуться как null .... Я бы сказал, что это проблема –