更新拦截器上的父亲

发布于 2024-08-07 19:33:30 字数 847 浏览 1 评论 0原文

我的模型:

 public class Father {
  Set<Son> sons = new HashSet<Son>();
  String name = null;
  Date lastSonModifyDate = null;
  // ... other fields and setters/getters
 }
 public class Son {
  Father father = null;
  String name = null;
  Date lastModifyDate = null;
  // ... other fields and setters/getters
 }

用例:

  1. 数据库中有一个 Father 对象,其关联有一个 Son 对象(bidir)。
  2. 从数据库父加载。
  3. 更新父亲的姓名字段。
  4. 更新儿子的姓名字段。
  5. 坚持父亲。

我的拦截器首先检测父亲更新(onFlushDirty)。然后为儿子执行onFlushDirty。在本例中,我更新了 son.lastModifyDate 和father.lastSonModifyDate

执行结束时,除father.lastSonModifyDate 之外的所有更新都会保留。我认为这是因为父亲正在会话中并且已在儿子之前更新,因此该实体会覆盖儿子实体的 onFlushDirty 方法中所做的更改。

我怎样才能达到我的目标(从儿子拦截器设置父亲的lastSonModifyDate)?

谢谢。

My model:

 public class Father {
  Set<Son> sons = new HashSet<Son>();
  String name = null;
  Date lastSonModifyDate = null;
  // ... other fields and setters/getters
 }
 public class Son {
  Father father = null;
  String name = null;
  Date lastModifyDate = null;
  // ... other fields and setters/getters
 }

Use case:

  1. There is in DB a Father object with a Son object associated (bidir).
  2. Load from DB father.
  3. Update name field for father.
  4. Update name field for son.
  5. Persist father.

My interceptor first detects father updates (onFlushDirty). Then executes the onFlushDirty for the son. In this case, I update son.lastModifyDate and also father.lastSonModifyDate.

When execution ends, all updates are persisted except father.lastSonModifyDate. I think this is because father is in session and has been updated before son, so this entity overrides the changes done in onFlushDirty method for the son entity.

How could I achieve my mark (set father's lastSonModifyDate from son interceptor)?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜尕妞 2024-08-14 19:33:30

你不能。 onFlushDirty() 是在为所有者调用 onFlushDirty() 并已计划更新操作(如果有)之后为集合元素调用的。

是否有任何原因导致您不能在 DAO 中执行上述所有操作而不是依赖拦截器?或者在数据库级别(映射生成的 lastModifyDate 属性)?

You can't. onFlushDirty() is invoked for collection elements after it has been invoked for owner and update action (if any) has already been scheduled.

Is there any reason why you can't do all of the above in your DAO instead of relying on interceptors? Or on the database level (mapping both lastModifyDate properties as generated)?

东京女 2024-08-14 19:33:30

您需要为具有级联选项的实体重写 onCollectionUpdate 。这是在安排之前调用的。

干杯~

you need to override onCollectionUpdate for entities that has cascading option. this is called before its being scheduled.

cheers~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文