'Query on sub properties of Embedded entity in datastore

I have user entity which has an embedded entity called address.Within address there is property called city. I want to query all the users in a particular city.

public class User implements Serializable {
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  String id;
  ...   
  EmbeddedEntity address;

 //Getter ,Setter
}

The data for embedded entity is set as

address.setProperty("Country","India");
address.setProperty("City","Chennai");

Query:

Query q = mgr.newQuery(User.class,"adderess.City == :arg0");
List<User> u = (List<User>) q.execute("Chennai");

It is not querying and throwing error

encountered a variable expression that isn't part of a join. maybe you're referencing a non-existent field of an embedded class.

So how to query and filter by property inside the embedded entity



Solution 1:[1]

From https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/datastore/EmbeddedEntity

This class is similar to Entity, but differs in the following ways:

  • equals(Object) and hashCode() compare the embedded properties in addition to the Key.
  • It is not queryable when stored in the datastore.

Converting your EmbeddedEntity to an Entity or another serializable object should let you query on the embedded fields.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jim Morrison