java.lang.Long不能转换为java.lang.Integer JPA GAE和枚举

时间:2014-05-16 17:17:12

标签: java google-app-engine jpa enums

我有JPA和GAE的问题。当我尝试对所有' Index_Book进行查询时,我得到 java.lang.Long无法转换为java.lang.Integer 异常。这导致我在Index类中的这一行

@Enumerated(EnumType.ORDINAL)
public Platform platform;

我想不要使用EnumType.STRING,因为我期待很多诱惑,只有3个不同的枚举。我在考虑使用转换器,但我不知道它对索引有什么影响。

我有实体看起来像这样。 DatastoreObject只是一些常用方法和其他东西的对象。

@Entity
@MappedSuperclass
public abstract class Index extends DatastoreObject {

    @Id
    private Key ID;

    @Extension(vendorName = "datanucleus", key = "gae.parent-pk", value ="true")
    public Key organizationID;

    @Enumerated(EnumType.ORDINAL)
    public Platform platform;

    ...
}

来自Index的派生类。

@Entity
public class Index_Book extends Index {

    ...
}

当我调用tq.getResultList()

时,这是我得到java强制转换异常的地方
Query tq =  em.createQuery(String.format("SELECT FROM %s s", "IndexBook"));


for(Index si : (List<Index>)tq.getResultList()){
    ...
}

知道如何解决这个问题吗? 捕获平台必须被索引。所以它也可以被搜索。

修改 这是完全例外:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
    at com.google.appengine.datanucleus.TypeConversionUtils.datastoreValueToPojoValue(TypeConversionUtils.java:403)
    at com.google.appengine.datanucleus.FetchFieldManager.fetchFieldFromEntity(FetchFieldManager.java:463)
    at com.google.appengine.datanucleus.FetchFieldManager.fetchObjectField(FetchFieldManager.java:408)
    at org.datanucleus.state.AbstractStateManager.replacingObjectField(AbstractStateManager.java:2353)
    at s4a.projectpinus.backend.shared.db.models.statistics.v2.abst.StatisticsIndex.jdoReplaceField(StatisticsIndex.java)
    at s4a.projectpinus.backend.shared.db.models.statistics.v2.StatisticsIndex_SessionTime.jdoReplaceField(StatisticsIndex_SessionTime.java)
    at s4a.projectpinus.backend.shared.db.models.statistics.v2.abst.StatisticsIndex.jdoReplaceFields(StatisticsIndex.java)
    at org.datanucleus.state.JDOStateManager.replaceFields(JDOStateManager.java:1935)
    at org.datanucleus.state.JDOStateManager.replaceFields(JDOStateManager.java:1962)
    at com.google.appengine.datanucleus.DatastorePersistenceHandler.fetchObject(DatastorePersistenceHandler.java:567)
    at org.datanucleus.state.JDOStateManager.loadFieldsFromDatastore(JDOStateManager.java:1638)
    at org.datanucleus.state.JDOStateManager.loadUnloadedFieldsInFetchPlan(JDOStateManager.java:1363)
    at org.datanucleus.state.JDOStateManager.detach(JDOStateManager.java:2718)
    at org.datanucleus.ObjectManagerImpl.performDetachOnCloseWork(ObjectManagerImpl.java:4571)
    at org.datanucleus.ObjectManagerImpl.performDetachOnClose(ObjectManagerImpl.java:4534)
    at org.datanucleus.ObjectManagerImpl.close(ObjectManagerImpl.java:1105)
    at org.datanucleus.api.jpa.JPAEntityManager.close(JPAEntityManager.java:193)
    at s4a.projectpinus.backend.core.DispatcherServlet.doGet(DispatcherServlet.java:148)
    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:127)
    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:366)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
    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:98)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:491)
    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)

1 个答案:

答案 0 :(得分:4)

在数据对象中使用Long而不是Integer,或者对从数据存储区检索到的值调用intValue()

数据存储将所有整数值存储为Longs。