【问题标题】:what is the current best method of getting the changes to an object hierarchy in Apache Cayenne?在 Apache Cayenne 中获取对象层次结构更改的当前最佳方法是什么?
【发布时间】:2019-10-10 08:38:11
【问题描述】:

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

我想知道是否有可能(对于单元测试断言)最好在调用 commitChanges 方法之前捕获测试代码对 ObjectContext(或某些底层数据表示)所做的更改,最好的做法是什么? 我有兴趣枚举待更新/待插入/待删除的对象或行,对于待更新的对象或行,我还对哪些属性/列实际更改感兴趣?

【问题讨论】:

    标签: apache-cayenne


    【解决方案1】:

    Cayenne 中的更改跟踪可以在多个级别进行:

    1. 最用户友好和最全面的 API,用于在“增量”之前和之后捕获 is provided bycayenne-commitlog,但它仅在提交期间有效。

    2. 在通过ObjectContext.uncommittedObjects()ObjectContext.newObjects()ObjectContext.deletedObjects()ObjectContext.modifiedObjects() 完成提交之前的任何时间在上下文中查找“脏”对象。

    3. 1234563 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);
    

    【讨论】:

    • 谢谢 Andrus,方法 #2 或 #3 对我来说都应该没问题。我对使用反射从对象中获取信息的测试没有任何问题 :-) 最好的问候,罗伯特
    猜你喜欢
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 2010-12-02
    相关资源
    最近更新 更多