事务中的祖先查询(objectify)返回“及时冻结”的数据

时间:2014-10-25 22:23:35

标签: java google-cloud-datastore objectify

直接举例:

@Entity
public class Parentt {
    @Id
    String id;
}

@Entity
public class Child {
    @Parent 
    Ref<Parentt> parenttRef;
    @Id
    String id;
}

public void test() {
final Parentt parentt = new Parentt();
        ofy().transact(new VoidWork() {
            @Override
            public void vrun() {

                parentt.setId(UUID.randomUUID().toString());
                ofy().save().entity(parentt).now();
                Child child = new Child();
                child.setId(UUID.randomUUID().toString());
                child.setParentt(parentt.getId());
                ofy().save().entity(child).now();

                LOG.info("query result size: " + ofy().load().type(Child.class).ancestor(parentt).list().size());
                ofy().flush();
                LOG.info("query result size: " + ofy().load().type(Child.class).ancestor(parentt).list().size());

            }
        });

        LOG.info("query result size(outside of transaction): " + ofy().load().type(Child.class).ancestor(parentt).list().size());
}

test()方法中的代码生成的结果:

query result size: 0

query result size: 0

query result size(outside of transaction): 1

我在Objectify文档中找到了:

  

低级API有一些怪癖:例如,get()和   query()s将看到数据存储区&#34;及时冻结&#34;并且不会反映   即使在交易中也会更新。 Objectify隐藏了这种行为   来自你;后续提取将看到相同的数据(或更新)   先前。请注意,由于查询始终在GAE内部运行,   索引(即过滤操作)似乎总是在时间上被冻结    - Objectify无法隐藏这一点。

祖先查询是否基于内部索引,这就是为什么查询在事务内部没有返回任何结果的原因(这里没有其他过滤除了父项)?

1 个答案:

答案 0 :(得分:2)

查询本质上基于索引 - 它们通过往返GAE来提供服务。因此,GAE的行为定义了返回值。 Objectify在这里真的没有别的。