【问题标题】:Objectify - how to create child entites with a parent Key that points to a nonexistant entityObjectify - 如何使用指向不存在实体的父键创建子实体
【发布时间】:2023-03-27 11:01:01
【问题描述】:

鉴于在 Objectify 中,实体组不是由类定义的,而是由实例定义的,并且您可以使用指向不存在实体的父 Key 创建子实体,您将如何做到这一点? (我在找一个简单的例子。上面的陈述来自Objectify文档,我很困惑。)

【问题讨论】:

    标签: objectify


    【解决方案1】:

    只需创建一个具有任意 id 的 Key。一个简单的例子:

    class Foo {
        @Parent Key<Par> parent;
        @Id Long id;
        // ...constructors, etc
    }
    
    // Create a parent key for which an entity may or may not exist, doesn't matter
    Key<Par> parent = Key.create(Par.class, 123L);
    
    Foo foo = new Foo(parent, someId);
    ofy().save().entity(foo).now();
    

    【讨论】:

    • 仅供参考,该方法最终不会为这样的获取返回实体:myEntity = ofy().load().type(POJO.class).id('id').safeGet( ); POJO 使用 @Parent 键持久化的地方,实体不存在,即在其构造函数中: this.parent = Key.create(POJO.class, anId);实体将持续存在,但不可获取。
    • ofy().load().type(POJO.class).parent(parentKey).id(id).safeGet()
    • 或者,只需构造完整的密钥并获取它:ofy().load().key(thefullyformedkey).safeGet()
    猜你喜欢
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多