JDO Queries with contains filter (ClassCastException)

859 views Asked by At

I am having trouble understanding JDO with the "contains" filter in the queries. I am working from the blog entry that was linked on the developers page on Google App Engine.

Every thing works great when i queried with string lists...

String JDO:

Query q = pm.newQuery(
"select from Person where :p1.contains(favoriteFood) order by favoriteFood, age");
q.execute(Arrays.asList("Vogon Grandmothers", "Pie"));

However i get a Exceptions when i try the same with integers...

List of Integers JDO:

Query q = pm.newQuery(
"select from Person where :p1.contains(favoriteNumber) order by favoriteNumber, age");
q.execute(Arrays.asList(42,99));

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer

Array of Integers JDO:

Query q = pm.newQuery(
"select from Person where :p1.contains(favoriteNumber) order by favoriteNumber, age");
q.execute(new Integer[]{42,99});

[Ljava.lang.Integer; cannot be cast to java.lang.Integer

UPDATE with all GAE source files

TestStuffServlet.java

package teststuff;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;

import javax.servlet.http.*;

@SuppressWarnings("serial")
public class TestStuffServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

    LinkedList<Person> personsWithFavoriteNumbers = Person.getPersonsWithFavoriteNumbers(Arrays.asList(1234));

    resp.setContentType("application/json");
    resp.getWriter().println(personsWithFavoriteNumbers);
    }
}

Person.java

package teststuff;

import java.util.LinkedList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

@PersistenceCapable
public class Person {

    @Persistent
    private String name;
    @Persistent
    private Integer favoriteNumber;

    public Person(String name, Integer favoriteNumber) {
        this.name = name;
        this.favoriteNumber = favoriteNumber;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getFavoriteNumber() {
        return favoriteNumber;
    }
    public void setFavoriteNumber(Integer favoriteNumber) {
        this.favoriteNumber = favoriteNumber;
    }

    public String toString(){
        return this.name + " " + this.favoriteNumber.toString();
    }

    public static Boolean savePerson(Person personToSave) {
        Boolean saveSuccessful = false;

        PersistenceManager pm = PMF.get().getPersistenceManager();

        try {
            pm.makePersistent(personToSave);
            saveSuccessful = true;
        } finally {
            pm.close();
        }

        return saveSuccessful;
    }

    public static LinkedList<Person> getPersonsWithFavoriteNumbers(List<Integer> favoriteNumbers) {
        LinkedList<Person> personsWithFavoriteNumbers = new LinkedList<Person>();

        PersistenceManager pm = PMF.get().getPersistenceManager();

        Query q = pm.newQuery(Person.class);

         q.setFilter(":p1.contains(favoriteNumber)");

        try {

            List<Person> results = (List<Person>) q.execute(favoriteNumbers);
            //List<Person> results = (List<Person>) q.execute(new Integer[]{2,3333});
              if (!results.isEmpty()) {
                for (Person p : results) {
                  // Process result g
                    personsWithFavoriteNumbers.add(p);
                }
              } else {
                // Handle "no results" case
              }
        } finally {
            q.closeAll();
        }

        return personsWithFavoriteNumbers;
    }

}

Stack Trace

HTTP ERROR 500

Problem accessing /teststuff. Reason:

    java.util.Arrays$ArrayList cannot be cast to java.lang.Integer
Caused by:    
java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer
    at com.google.appengine.datanucleus.TypeConversionUtils$8.apply(TypeConversionUtils.java:161)
    at com.google.appengine.datanucleus.TypeConversionUtils$8.apply(TypeConversionUtils.java:156)
    at com.google.appengine.datanucleus.TypeConversionUtils.pojoValueToDatastoreValue(TypeConversionUtils.java:810)
    at com.google.appengine.datanucleus.query.DatastoreQuery.addLeftPrimaryExpression(DatastoreQuery.java:1131)
    at com.google.appengine.datanucleus.query.DatastoreQuery.handleContainsOperation(DatastoreQuery.java:969)
    at com.google.appengine.datanucleus.query.DatastoreQuery.addExpression(DatastoreQuery.java:830)
    at com.google.appengine.datanucleus.query.DatastoreQuery.addFilters(DatastoreQuery.java:739)
    at com.google.appengine.datanucleus.query.DatastoreQuery.compile(DatastoreQuery.java:248)
    at com.google.appengine.datanucleus.query.JDOQLQuery.performExecute(JDOQLQuery.java:158)
    at org.datanucleus.store.query.Query.executeQuery(Query.java:1789)
    at org.datanucleus.store.query.Query.executeWithArray(Query.java:1666)
    at org.datanucleus.api.jdo.JDOQuery.execute(JDOQuery.java:243)
    at teststuff.Person.getPersonsWithFavoriteNumbers(Person.java:66)
    at teststuff.TestStuffServlet.doGet(TestStuffServlet.java:14)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:368)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:351)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:485)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
0

There are 0 answers