简体   繁体   English

GAE Objectify在同一事务中的事务中创建的加载对象

[英]GAE Objectify load object created in transaction within same transaction

I try to use objectify transaction, but I have some issues when I need to reload an object created in the same transaction. 我尝试使用objectify事务,但是当我需要重新加载在同一事务中创建的对象时,我遇到了一些问题。

Take this sample code 拿这个示例代码

@Entity
public class MyObject
{
    @Parent
    Key<ParentClass> parent;

    @Index
    String foo;
}


ofy().transact(new VoidWork()
{
    @Override
    public void vrun()
    {
        ParentClass parent = load();// load the parent
        String fooValue = "bar";

        Key<ParentClass> parentKey = Key.create(ParentClass.class, parent.getId())
        MyObject myObject = new MyObject(parentKey);
        myObject.setFoo(fooValue);

        ofy().save().entity(myObject).now();

        MyObject reloaded = ofy().load().type(MyObject.class).ancestor(parentKey).filter("foo", fooValue).first().now();

        if(reloaded == null)
        {
            throw new RuntimeException("error");
        }
    }
});

My object reloaded is always null, maybe I miss something, but logically within a transaction I can query an object which was created in the same transaction? 我的对象重新加载总是为null,也许我想念一些东西,但在逻辑上我可以查询在同一个事务中创建的对象?

Thanks 谢谢

Cloud Datastore differs from relational databases in this particular case. 在这种特定情况下,云数据存储区与关系数据库不同。 The documentation states that - 文件说明 -

Unlike with most databases, queries and gets inside a Cloud Datastore transaction do not see the results of previous writes inside that transaction. 与大多数数据库不同,查询和获取Cloud Datastore事务不会在该事务中看到先前写入的结果。 Specifically, if an entity is modified or deleted within a transaction, a query or lookup returns the original version of the entity as of the beginning of the transaction, or nothing if the entity did not exist then. 具体而言,如果在事务中修改或删除实体,则查询或查找将在事务开始时返回实体的原始版本,如果实体不存在则返回任何内容。

https://cloud.google.com/datastore/docs/concepts/transactions#isolation_and_consistency https://cloud.google.com/datastore/docs/concepts/transactions#isolation_and_consistency

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM