Search code examples
javagoogle-app-enginegoogle-cloud-datastoreobjectify

GAE w/ Objectify - Can you query a HashMap?


In GAE, when using Objectify, can you query a HashMap? If so how would would you write it?

ofy().load().type(MyClass.class).filter("hashMapfieldName", "keyQueryinggFor").list();

Does not seem to work where the hashMapfieldName is a HashMap<String, String>. I am looking to find entities where hashMapfieldName contains a certain key.


Solution

  • Just like embedded classes, Objectify converts Map<String, String> to the low-level EmbeddedEntity object, which is not indexible. However, if you @Index your Map field (or embedded class field), Objectify will create a synthetic index that lets you query anyways.

    Following your example, let's say you have a Map field named hashMapfieldName containing the mapping of strings "key" to "value". This query syntax will return entities that have the pair:

    ofy().load().type(MyClass.class).filter("hashMapfieldName.key", "value");
    

    If you are just looking for key existence, try filter("hashMapfieldName.key !=", null).