Application boot file - query on Enum fields

I am using GAE (Java) with JDO to save.

I have an object with an Enum field that is marked as @Persistent and is correctly stored in the data store (as seen from the data store viewer in the development console). But when I request these entities that place the filter based on the Enum value, it always returns me all entities regardless of the value that I specify for the enum field.

I know that GAE java supports enumerations that are preserved in the same way as basic data types. But also allows you to receive or request them? A Google search could not point me to such an example code.

Details:

I printed the request just before it was executed. Thus, in two cases, the query looks like this:

SELECT FROM com.xxx.yyy.User WHERE role == super ORDER BY key desc RANGE 0,50 SELECT FROM com.xxx.yyy.User WHERE role == admin ORDER BY key desc RANGE 0,50 

Both of the above queries return all User objects from the data store to me, despite the data store viewer showing that some users are of type β€œadmin” and some of them are of type β€œsuper”.

+6
java enums google-app-engine google-cloud-datastore
source share
3 answers

For a while, I replaced Enums with simple integer constants. This case is reported as a problem in the Google engine: http://code.google.com/p/googleappengine/issues/detail?id=2927

+3
source share

For a parameter other than String or int, I believe that you need to use declareParameters. Try something like this:

 Query q = pm.newQuery(com.xxx.yyy.User.class); q.setFilter("role == p1"); //p1 is a variable place holder q.declareParameters("Enum p1"); //here you define the data type for the variable, in this case an Enum q.setRange(0, 50); q.setOrdering("key desc"); AbstractQueryResult results = (AbstractQueryResult) pm.newQuery(q).execute(admin); 

or if you want more gql syntax -

 Query query = pm.newQuery("SELECT FROM com.xxx.yyy.User WHERE role == p1 ORDER BY key desc RANGE 0,50"); query.declareParameters("Enum p1"); AbstractQueryResult results = (AbstractQueryResult) pm.newQuery(q).execute(admin); 
+3
source share

You need to use the name of the enumeration class when declaring the request parameter.

For example, if you create your query using a method style and think your enum is called Role and declared in the User class, you can do something like the following:

 Query query = pm.newQuery(com.xxx.yyy.User.class); query.setFilter("role == roleParam"); query.declareParameters(com.xxx.yyy.User.Role.class.getName() + " roleParam"); 
+2
source share

All Articles