Get an Objectify Entity's Key

后端 未结 3 777
陌清茗
陌清茗 2021-01-12 11:09

Dummy question.

I create my POJO Objectify entity (for example, \"Category\") and persist it.

Then I retrieve it via a query.

I want to use it in a

相关标签:
3条回答
  • 2021-01-12 11:28

    The Key class in Objectify 4 has this method:

    public static <T> Key<T> create(T pojo)
    

    so, if you already have read the entity (called category in this example) from the datastore, you can just call

    product.categoria = Key.create(category);
    
    0 讨论(0)
  • 2021-01-12 11:32

    For objectify 4 use:

    public Key<Foo> getKey() {
       return Key.create(Foo.class, id);
    }
    

    Or if the entity has a @Parent

    public Key<Bar> getKey() {
       return Key.create(parentKey, Bar.class, id);
    }
    
    0 讨论(0)
  • 2021-01-12 11:42

    I'm usually adding an extra method:

    @Transient
    Key<Categoria> getKey() {
       return Key.create(Categoria.class, id);
    }
    

    and use it where it needed:

    anCategoria.getKey()
    
    0 讨论(0)
提交回复
热议问题