1

我的程序中有以下关系:

@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;
    }
    ...
}

当我用他的文档信息保存学生时,一切似乎都很好。当我从数据库中读取他时,一切似乎也很好,但是当我再次尝试读取它时,我有 NullPointerException。我做了一些研究,发现在第一次调用student.getDocuments()hibernate 时会从表中删除记录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=?

我做错了什么?谢谢

我的测试代码如下所示:

    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

服务方式:

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);
}
4

1 回答 1

0

好像我刚刚发现了这个问题。现在无法在我的旧项目中检查它,但它与与此 hibernate bug相关的问题非常相似。

于 2014-02-18T13:17:08.993 回答