Get Objectify Object Key

Fake question.

I create a POJO Objectify (for example, "Category") and save it.

Then I get it with a request.

I want to use it in a relationship to one another, for example. want to set my category to one or more "Products".

I will have this in my "product": Key<Categoria> categoria;

So the question is: how can I find my extracted entity key to install it in my product?

+7
source share
3 answers

Usually I add an additional method:

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

and use it where necessary:

 anCategoria.getKey() 
+9
source

For objective 4 use:

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

Or if the object has @Parent

 public Key<Bar> getKey() { return Key.create(parentKey, Bar.class, id); } 
+13
source

The Key class in Objectify 4 has this method:

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

therefore, if you have already read an object (called category in this example) from the data store, you can simply call

 product.categoria = Key.create(category); 
+1
source

All Articles