5

I have a spring boot project with some entities, in specific, I have a Student Class with a DesiredCourses list which should be a Set<>.

When I use:

@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
public List<StudentDesiredCourseEntity> getStudentDesiredCourses() {
    return studentDesiredCourses;
}

public void setStudentDesiredCourses(List<StudentDesiredCourseEntity> studentDesiredCourses) {
    this.studentDesiredCourses = studentDesiredCourses;
}

Everything works fine but when I use

@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
public Set<StudentDesiredCourseEntity> getStudentDesiredCourses() {
    return studentDesiredCourses;
}

public void setStudentDesiredCourses(Set<StudentDesiredCourseEntity> studentDesiredCourses) {
    this.studentDesiredCourses = studentDesiredCourses;
}

I get

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])

Is there something I am missing or something extra that needs to be done?

As requested, the equals and hashcode

@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;
}
3
  • 1
    I am wondering if the HashCode or Equals method may be the culprit do you have the HashCode and Equals for the StudentDesiredCourseEntity? Commented Apr 19, 2015 at 4:26
  • Posted the equals and hash code Commented Apr 19, 2015 at 14:59
  • Ahhhh you know what, I never considered that my Student or Course could come back as null.... I'd bet that is the problem Commented Apr 19, 2015 at 15:05

2 Answers 2

4

As mentioned by alexwen in comments, the reason this didn't work was due to not handling nulls in the hashcode/equals methods

Sign up to request clarification or add additional context in comments.

Comments

2

Jackson can't convert a json array into a hashSet. In order to do that, you need to create a custom Jackson Converter. Here is an example http://kdubblabs.com/java/retrofit-by-square/retrofit-using-jackson-json-conversion/

2 Comments

I would think that is is a common enough request that it would be auto handled....
Jackson supports Sets, I think something else may be going on here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.