3

I have a class with an enum as instance variable:

  public enum Races {
     Human, Elf, Orc, Troll
  }

  @PersistenceCapable(detachable="true")
  public class Crafter {
     @PrimaryKey
     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
     private Key key;
     @Persistent
     private Races race;
  }

I have been able to store and retrieve instances of that class, this is not the problem.

The problem comes when I want to query on, let's say, all the orcs and troll.

  Races[] races = new Races[] { Races.Orc, Races.Troll, };

  Query query = manager.newQuery(Crafter.class);
  query.setFilter(":raceParam.contains(race)");
  List<Crafter> crafters = (List<Crafter>) query.execute(Arrays.asList(races));

It doesn't work, I get:

    Caused by: java.lang.IllegalArgumentException: race: ca.forklabs.wowtradeskills.web.shared.Races is not a supported property type.
      at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedSingleValue(DataTypeUtils.java:184)
      at com.google.appengine.api.datastore.DataTypeUtils.checkSupportedValue(DataTypeUtils.java:149)
      at com.google.appengine.api.datastore.Query$FilterPredicate.<init>(Query.java:574)
      at com.google.appengine.api.datastore.Query.addFilter(Query.java:260)
      at org.datanucleus.store.appengine.query.DatastoreQuery.addLeftPrimaryExpression(DatastoreQuery.java:1343)
            ...

How does one do IN queries using JDO with the Google App Engine?

Some references:

2 Answers2

0

An old question but I have successfully been doing contains() in JDOQL (DataNucleus implementation) using the 'matches' method:

matches(String pattern)

Returns whether string matches the passed expression. The pattern argument follows the rules of java.lang.String.matches method. Only the following regular expression patterns are required to be supported and are portable: global “(?i)” for case-insensitive matches; and “.” and “.*” for wild card matches. The pattern passed to matches must be a literal or parameter.

For example:

To add a filter clause that checks if the companyName attribute 'contains' the text stored in criteria in a case insensitive way:

query.setFilter("companyName.matches(\"(?i).*" + criteria + ".*\")");

Check out the methods section on this page:

https://www.datanucleus.org/products/accessplatform/jdo/query.html#jdoql_methods

Volksman
  • 1,969
  • 23
  • 18
0

contains in JDOQL is defined like in Java ... on a Collection (field). You have an array. JDOQL syntax follows Java syntax

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • I am not following you. I am passing a list to the execute() method as a parameter and calling the contains() on my parameter. What am I doing wrong? – user1091789 Dec 11 '11 at 16:59
  • ahh, yes. So maybe GAE version you're using doesn't support a Collection parameter; all I can comment on is validity of the JDOQL – DataNucleus Dec 11 '11 at 17:14
  • mr datanucleus, I have the last version of GAE with datanucleus datanucleus-api-jdo-3.1.0 and this doesn't work! any thoughts? even on strings. – Rafael Sanches Sep 15 '12 at 08:37