启用:将子级添加到父级的集合时触发实体更改

时间:2018-07-04 11:01:52

标签: java hibernate-envers

我有一个Parent.java类:

@Entity
@Audited
public class Parent implements Serializable {

    private Set<Child> children = new HashSet<>();

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    @Where(clause = "deleted = 'FALSE'")
    public Set<Child> getChildren() {
        return children;
    }

    public void setChildren(Set<Child> children) {
        this.children = children;
    }

    public void addChild(Child child) {
       children.add(child);
       child.setParent(this);
    }
}

和一个具有映射关系的Child.java,如下所示:

@Entity
@Audited
public class Child implements Serializable {

    private Integer parentId;
    private Parent parent;

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parentId", referencedColumnName = "parentId")
    public Parent getParent() {
        return parent;
    }

    public void setParent(Parent parent) {
        this.parent = parent;
    }
}

ParentDao.java类:

public class ParentDao {

   @Inject
   protected EntityManager em;

   public Parent updateParent(Parent parent) {
      for (Child child : parent.getChildern()) {
         parent.addChild(child);
      }

      Parent merged = em.merge(parent);

      em.flush();
      em.refresh(merged);

      return merged;
   }
}

我认为将新的子级添加到父级的集合中会触发EntityTrackingRevisionListener.java类中的entityChanged方法,但是不会发生。

在这种情况下,我的代码或entityChanged中是否缺少任何内容?

0 个答案:

没有答案