【问题标题】:Eclipse link alternative to @PreUpdate annotation?Eclipse 链接替代 @PreUpdate 注释?
【发布时间】:2012-04-11 07:31:05
【问题描述】:

我一直在处理 Entity,oneToMany 关系,问题是每当我使用它时:

//Parent class    

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER, 
        targetEntity = FeedbackCategory.class, mappedBy = "parent")    
@PrivateOwned
@OrderBy("category ASC")
private List<Child> children;

//... other code here, i.e. getters-setters

@PrePersist
@PreUpdate
void prePersistUpdate() {

  // set the foreign key of child to this.ID
  if(children != null && !children.isEmpty())
  {
      for(Child ch: children)
      {
          ch.setParent(this);       
      }          
  }
}

在更新 Parent.class 时,尤其是在干净更新实体时,Parent 的 Id 没有与子实体(作为外键)一起保存。请帮忙...

似乎@PreUpdate 不起作用,@PrePersist 完全起作用。

【问题讨论】:

    标签: java jpa orm entity eclipselink


    【解决方案1】:

    一般来说,正确维护模型会更好。

    添加一个 addChild() 方法来添加子元素并设置父元素。那么你的模型就不会损坏了。

    @PreUpdate 发生在提交过程的后期(在确定需要更新对象之后)。我认为没有更早的 JPA 事件,但您可以使用 EclipseLink PreWrite 事件。为此,您需要使用 DescriptorEventListener,其配置方式与 EntityListener 相同。

    【讨论】:

    • 我通过不使更新成为一个干净的更新来管理它(意思是,不设置 parentID 并在@PreUpdate 上设置 ch.setParent(this))。如果您不取消引用并再次引用您的子实体,@PreUpdate 似乎正在工作。不过,谢谢!
    猜你喜欢
    • 2016-03-20
    • 2015-10-01
    • 2019-06-30
    • 2014-11-05
    • 2013-12-31
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2011-09-21
    相关资源
    最近更新 更多