user3358218 user3358218 - 1 year ago 81
Java Question

How to check if an entity exists in the Datastore in Java?

Okay, the scenario is the following:

I have an Entity of kind

with a property

Entity email = new Entity("Email");
email.setProperty("name", "John");

What I want to check is if exists an entity of kind
with the property
equal to
. It is possible to make that in Java?

Thank you.

Answer Source

Property "name" is indexed, so you can run a query on Email entity to check if an entity (or entities) with this name already exist. For example:

Query q = new Query("Email");
q.setFilter(new FilterPredicate("name", FilterOperator.EQUAL, "John");
List<Entity> entities = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
if (entities.isEmpty()) {
    // no entities with name "John"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download