简体   繁体   English

如何强制TopLink立即建立OneToMany关系?

[英]How to force TopLink to set up OneToMany relation immediately?

I have two entites, Person and Teacher , with a OneToMany relationship: 我有两个实体, PersonTeacher ,具有一对多关系:

public class Person implements Serializable {
    @Id
    @Column(nullable = false)
    private String login;
    @OneToMany(mappedBy = "person")
    private List<Teacher> teacherList;
}

public class Teacher implements Serializable {
    @ManyToOne
    @JoinColumn(name = "LOGIN")
    private Person person;
}

I also have a DTO class for Person : 我还有一个Person的DTO课程:

public class PersonDTO extends Person {
    private boolean isTeacher;

    public PersonDTO(Person p) {
        setLogin(p.getLogin());
        isTeacher = getTeacherList().size() > 0;
    }

    public boolean isTeacher() {
        return isTeacher;
    }
}

My EJB has code like this for inserting new Teacher s and reading Person s: 我的EJB具有这样的代码,用于插入新的Teacher和读取Person

public void addTeacher(String login, String password, String name,
                       String dept, String title) {
    p = new Person(true, false, login, name, password);
    persistEntity(p);
    Teacher t = new Teacher(dept, p, title);
    persistEntity(t);
    em.flush();    // 'em' is the Entity Manager
}

public PersonDTO readPerson(String login) {
    Person p = em.find(Person.class, login);
    PersonDTO pdto = new PersonDTO(p);
    return pdto;
}

On my webpage, there's a table showing all teachers, textboxes for the various fields, and a button to add a new teacher and update the table, the listener for which looks like this: 在我的网页上,有一个显示所有教师的表格,各个字段的文本框以及一个添加新教师并更新表格的按钮,其监听器如下所示:

public void addTeacherListener(ActionEvent actionEvent) {
    adminManager.addTeacher(/* parameters */);

    // update table (which is bound to 'teacherList')
    teacherList = new LinkedList<Teacher>();
    List<String> logins = adminManager.findAllPerson();
    for (String login : logins) {
        PersonDTO pdto = adminManager.readPerson(login);
        if (pdto.isTeacher()) {
          teacherList.add(pdto.getTeacherList().get(0));
        }
    }
}

The problem is that after inserting a new Teacher, it doesn't show up in the table. 问题是插入新的教师后,它不会显示在表格中。 The precise reason for this is that the PersonDTO object returned by readPerson has isTeacher set to false: which means that at the time the DTO object is created (that is, after the insertion operation finished, including the two persists and the flush at the end), the Person object for the given teacher has an empty teacherList . 造成这种情况的确切原因是PersonDTO由返回的对象readPerson具有isTeacher设置为false:这意味着此时的DTO对象被创建(即,后插入操作完成后,包括两个仍然存在且齐平于该端),给定教师的Person对象具有一个空的teacherList

However, if I close the page and relaunch it from the IDE, the newly inserted elements show up in the table. 但是,如果我关闭该页面并从IDE重新启动,则新插入的元素将显示在表中。 So apparently TopLink does set up the relationship, just not immediately. 因此,显然TopLink确实建立了关系,只是没有立即建立关系。 What exactly is going on, and how can I force it to do it right after the insertion? 到底发生了什么,如何在插入后立即强制执行该操作?

JPA doesn't maintain consistent bidirectional relationships between object in memory automatically, it's your own task. JPA不会自动在内存中的对象之间保持一致的双向关系,这是您自己的任务。

That is, after executing addTeacher() you have a Teacher with person property pointing to the Person . 也就是说,在执行addTeacher()您将获得一个Teacher ,其person属性指向Person This relationship is reflected in the database, but it's not reflected in teacherList of the Person that is already in memory. 此关系反映在数据库中,但不反映在已经在内存中的Person teacherList中。 em.find() return the existing instance of Person from the persistence context, so its teacherList is iempty. em.find()从持久性上下文返回Person的现有实例,因此其teacherList是空的。

Therefore you need to add a Teacher into Person.teacherList in addTeacher() . 因此,您需要在addTeacher() Person.teacherList中添加一个Teacher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM