GAE w / Objectify - Can you request a HashMap?

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

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

      

Doesn't seem to work where hashMapfieldName

is HashMap<String, String>

. I am looking to find objects where hashMapfieldName

a specific key contains.

+3


source to share


3 answers


Just like the built-in classes, Objectify converts Map<String, String>

to a low-level object EmbeddedEntity

that is not indexed. However, if you @Index on a field Map

(or a nested class field), Objectify will create a synthetic index that will allow you to execute the query anyway.

Following your example, let's say you have a Named Map field hashMapfieldName

containing a mapping of rows "key"

to "value"

. This query syntax returns objects that have a pair:



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

      

If you're just looking for the existence of a key, give it a try filter("hashMapfieldName.key !=", null)

.

+7


source


I have a similar case where I have stored device specific values ​​in an embedded hashmap where the device id is the key and I need to query for all objects that contain a specific value.

The suggested solution above actually works in my test environment. But I realized that I could not use this real application as it would not scale for the number of users we are designing. This is because Objectify will generate a property index for each hashmap key that I save . In my case, this means that each user device will have its own index, which could be 100,000 indexes!



So I went for this alternative approach instead:

  • in the method @OnSave

    for the object, I store the values ​​that I want to find in a separate indexed list that I only use for queries
  • then I will make my filtered request like this: ofy().load().type(MyEntity.class).filter("values", value)

+1


source


I'm not sure what Objectify

for HashMap

.

But according to the docs the HashMap

field type is not supported in Datastore

. Even if we serialize

save it, it will be saved as Blob, which is the default unindexded

.

As far as I know, HashMap

cannot be queried because no indexes have been created for it.

0


source







All Articles