Bharathwaj ravi
Bharathwaj ravi

Reputation: 1

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

Upvotes: 0

Views: 224

Answers (1)

Jim Morrison
Jim Morrison

Reputation: 2887

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.

Upvotes: 0

Related Questions