gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:52 25 4
gpt4 key购买 nike

我尝试使用 objectify 事务,但是当我需要重新加载在同一事务中创建的对象时遇到一些问题。

获取此示例代码

@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");
}
}
});

我重新加载的对象始终为空,也许我错过了一些东西,但从逻辑上讲,在事务中我可以查询在同一事务中创建的对象?

谢谢

最佳答案

在这种特殊情况下,Cloud Datastore 与关系数据库不同。该文档指出 -

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

关于java - GAE Objectify 加载在同一事务内的事务中创建的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39795207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com