JDO - 在将模型对象传递给视图之前,我是否需要对其调用 DetachCopy?

发布于 2024-08-20 19:28:08 字数 1141 浏览 6 评论 0原文

我对分离复制的理解是,它会创建对象的副本,以便您可以在 PersistenceManager 不注意的情况下对其进行更改。

由于我在将模型对象传递到要使用的视图之前关闭了 PersistenceManager,因此在传递它之前我不必调用 detachCopy 或 makeTransient 之类的东西,不是吗?

我看过的例子确实称它为... 这是我从 http://code.google 查看的示例。 com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html

public Employee getEmployee(User user) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    Employee employee, detached = null;
    try {
        employee = pm.getObjectById(Employee.class,
            "[email protected]");

        // If you're using transactions, you can call
        // pm.setDetachAllOnCommit(true) before committing to automatically
        // detach all objects without calls to detachCopy or detachCopyAll.
        detached = pm.detachCopy(employee);
    } finally {
        pm.close();
    }
    return detached;
}

My understanding of detach copy is that it makes a copy of your object so that you can make changes to it without the PersistenceManager noticing.

Since I close my PersistenceManager before passing the model object to the view to be used, I wouldn't have to call anything like detachCopy or makeTransient before passing it along would I?

The examples I looked at do call it though...
This is the example I looked at from http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html:

public Employee getEmployee(User user) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    Employee employee, detached = null;
    try {
        employee = pm.getObjectById(Employee.class,
            "[email protected]");

        // If you're using transactions, you can call
        // pm.setDetachAllOnCommit(true) before committing to automatically
        // detach all objects without calls to detachCopy or detachCopyAll.
        detached = pm.detachCopy(employee);
    } finally {
        pm.close();
    }
    return detached;
}

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

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

发布评论

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

评论(2

ゃ懵逼小萝莉 2024-08-27 19:28:08

您可以使用 PMF 属性自动分离对象,或者手动分离它们的副本,如示例所示。现在问题是什么?

You can have objects detached automatically using the PMF prop, or detach copies of them manually, as the example says. Now what was the question?

木格 2024-08-27 19:28:08

来自 http://db.apache.org/jdo/attach_detach.html

分离的对象保留其数据存储实体的 ID。独立的
应该在要更新对象并附加的地方使用对象
稍后更新它们(更新数据存储中的关联对象。如果您
想要使用自己的数据存储中的对象创建副本
身份,您应该使用 makeTransient 而不是 detachCopy。

From http://db.apache.org/jdo/attach_detach.html

A detached object retains the id of its datastore entity. Detached
objects should be used where you want to update the objects and attach
them later (updating the associated object in the datastore. If you
want to create copies of the objects in the datastore with their own
identities you should use makeTransient instead of detachCopy.

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