Say I have made Objectify4 work with my project (although I am still working on making it work) is it possible to persist an Entity with Map field like this:
class Foo {
Map<String, Object> map;
public Foo() {}
public Foo(Map map) {
this.map = map;
}
}
The basic idea is to be able to store/persist a “generic” field that can be either
- java.lang.String
- java.lang.Number
- java.lang.Boolean
- null
- java.util.List
- java.util.Map
For example I can store this:
map.put("uid:sarah:fname", "Sarah");
persist(new Foo(map));
Or
map.put("uid:sarah:age", 25);
persist(new Foo(map));
Is this even possible with Objectify? or App Engine?
This will work as-is with Objectify4, with a couple caveats:
Objectvalue will not be translated at all; it will be whatever the datastore accepts.1 is because this ends up being stored in the raw Entity as
field.keyname(with possibly several layers of ‘.’ separating embedded fields).2 is because you’re removing any type information so Objectify just treats Object as-is. It’s like you’re using the low-level api directly; you can only store primitives or collections of primitives. No arrays, certainly no Maps. Collections always come back as List. Numbers always come back as Long no matter what size they are stored as. There are some other quirky behaviors too.
If you really want this behavior, you can create a custom TranslatorFactory which will introspect the types at runtime and do just about anything you want. This is pretty advanced, however, and will require some study of the existing translators. We can help you out on the Google Group; stackoverflow is probably not the right place for this discussion.