在 Apache Cayenne 中获取对象层次结构更改的当前最佳方法是什么?

what is the current best method of getting the changes to an object hierarchy in Apache Cayenne?

我正在为一些新项目评估 Cayenne。

我想知道是否有可能(对于单元测试断言)捕获测试代码对 ObjectContext(或某些底层数据表示)所做的更改,最好是在调用 commitChanges 方法之前,它的最佳实践是什么? 我对枚举 to-be-updated/to-be-inserted/to-be-deleted 对象或行以及待更新的对象或行感兴趣 我也对 attributes/columns 实际更改的内容感兴趣?

Cayenne 中的更改跟踪可以在多个级别完成:

  1. 最人性化最全面的API前后抓取"deltas"is provided bycayenne-commitlog,但只在commit时有效。

  2. 在通过 ObjectContext.uncommittedObjects()ObjectContext.newObjects()ObjectContext.deletedObjects()、[=15= 完成提交之前的任何时间在上下文中查找 "dirty" 个对象].

  3. 如果你想更深入地了解#2,并在提交之前跟踪对对象的个别更改,你可以查看 ObjectStore,尽管这个 API 是针对内部框架的使用,所以它需要一些跳跃。 Cayenne 方面肯定有改进的空间:

ObjectStore os = (ObjectStore) context.getGraphManager();

// get DB snapshot of an object that can be compared with the object current state
DataRow previousState = os.getSnapshot(o.getObjectId());

// also there's a full diff available, but unfortunately "getChanges" is package-private,
// so can only be accessed via some reflection hacks (or a custom utility class placed 
// in "org.apache.cayenne.access" package). 
// Cayenne should make it public in the future I think.
GraphDiff diff = os.getChanges();
diff.apply(myChangeTrackingVisitor);