简体   繁体   中英

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.

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?

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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