Katedral Pillon Katedral Pillon - 1 month ago 17
Java Question

GQL query where field is not null

I have an entity kind called Account. One of the fields is a String named

selfie
, which is basically the url to a selfie uploaded by a user. I want to fetch for users who have a selfie (so if a user does not have a selfie they should not be included in the result set). I have the following query. but it won't work because I have
"NULL"
as a string. What is the correct way for doing this? Again, I only want users who have selfies.

Filter selfie = new FilterPredicate("selfie", FilterOperator.GREATER_THAN, "NULL");
Query query = new Query(Account.class.getSimpleName()).setFilter(selfie);
FetchOptions options = FetchOptions.Builder.withLimit(30);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
QueryResultList<Entity> entities = datastore.prepare(query).asQueryResultList(options);


Also I am open to a JDO/JPA way of doing this on App-Engine (but it must work on App-Engine).

Answer

This should be as simple as

Filter selfie = new FilterPredicate("selfie", FilterOperator.NOT_EQUAL, null);

where you actually pass the Java null value (not a String) and filter with not equals (see http://stackoverflow.com/a/22200137).

Personally, I've been using JDO for my datastore management, which has its pros and cons. Let me know if you'd like to see a solution using JDO as well. Cheers!

Comments