2015-04-18 51 views
3

我有一個實體的彈簧引導項目,在具體的,我有一個學生類與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; 
} 
+1

我想知道如果哈希碼還是equals方法可能是罪魁禍首你是否擁有StudentDesiredCourseEntity的HashCode和Equals? – alexwen

+0

發表了等號和哈希碼 –

+0

Ahhhh你知道什麼,我從來沒有考慮過我的學生或課程可能會返回爲空....我敢打賭,這是問題 –

回答

2

正如在評論中提到由alexwen,這沒有工作的原因是由於哈希碼/不處理空值equals方法

相關問題