I have following relation in my program:
@Entity
@Table(name = "student")
public class Student extends Person {
private Map<Document, DocumentInfo> documents =
new HashMap<Document, DocumentInfo>();
...
@OneToMany
@JoinTable(name = "student_documents",
joinColumns = @JoinColumn(name = "student"),
inverseJoinColumns = @JoinColumn(name = "document_info"))
@MapKeyJoinColumn(name = "document")
public Map<Document, DocumentInfo> getDocuments() {
return documents;
}
...
}
When I save student with his documents information all seems good.. When I read him from database all seems good too, but when I try to read it one more time I have NullPointerException. I did some research and found that during the first invocation of student.getDocuments()
hibernate deletes records from table student_documents
:
Hibernate: select document0_.id as id2_0_, document0_.title as title2_0_ from document document0_ where document0_.id=?
Hibernate: select document0_.id as id2_0_, document0_.title as title2_0_ from document document0_ where document0_.id=?
Hibernate: delete from student_documents where student=?
What I've done wrong? Thanks
my test code looks like:
Student student = new Student();
student.setFirstName("123");
studentService.add(student);
Student student2 = studentService.read(student.getId());
assertFalse(student2.getDocuments().size() == 0); // here it all occurs
service method:
public Student add(Student student) {
for (Document document : documentRepository.findAll()) {
DocumentInfo documentInfo = new DocumentInfo(false, "");
documentInfoRepository.save(documentInfo);
student.getDocuments().put(document, documentInfo);
}
return studentRepository.save(student);
}
Seems like I just found the issue. Can't check it now in my old project, but it's very similar to problems related to this hibernate bug .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.