Question

I've got an application that I'm running on GAE, using JPA to access the datastore.

I'm getting a class cast exception on ONE of my entity types when trying to retrieve. Exception is:

Caused by: java.lang.ClassCastException: org.datanucleus.store.types.sco.simple.Date cannot be cast to java.lang.Long
    at com.annkh.entities.PurchasedQuote.jdoReplaceField(PurchasedQuote.java)
    at org.datanucleus.state.AbstractStateManager.replaceField(AbstractStateManager.java:2387)
    at org.datanucleus.state.JDOStateManager.replaceField(JDOStateManager.java:1877)
    at org.datanucleus.state.JDOStateManager.replaceField(JDOStateManager.java:1781)
    at org.datanucleus.store.types.sco.SCOUtils.createSCOWrapper(SCOUtils.java:241)
    at org.datanucleus.store.types.sco.SCOUtils.newSCOInstance(SCOUtils.java:139)
    at org.datanucleus.state.JDOStateManager.wrapSCOField(JDOStateManager.java:2230)
    at com.google.appengine.datanucleus.FetchFieldManager.fetchFieldFromEntity(FetchFieldManager.java:468)
    at com.google.appengine.datanucleus.FetchFieldManager.fetchObjectField(FetchFieldManager.java:408)
    at org.datanucleus.state.AbstractStateManager.replacingObjectField(AbstractStateManager.java:2353)
    at com.annkh.entities.PurchasedQuote.jdoReplaceField(PurchasedQuote.java)
    at com.annkh.entities.PurchasedQuote.jdoReplaceFields(PurchasedQuote.java)
    at org.datanucleus.state.JDOStateManager.replaceFields(JDOStateManager.java:1935)
    at org.datanucleus.state.JDOStateManager.replaceFields(JDOStateManager.java:1962)
    at com.google.appengine.datanucleus.EntityUtils$1.fetchFields(EntityUtils.java:974)
    at org.datanucleus.state.JDOStateManager.loadFieldValues(JDOStateManager.java:764)
    at org.datanucleus.state.JDOStateManager.initialiseForHollow(JDOStateManager.java:205)
    at org.datanucleus.state.StateManagerFactory.newForHollowPopulated(StateManagerFactory.java:89)
    at org.datanucleus.state.ObjectProviderFactory.newForHollowPopulated(ObjectProviderFactory.java:75)
    at org.datanucleus.ObjectManagerImpl.findObject(ObjectManagerImpl.java:2882)
    at com.google.appengine.datanucleus.EntityUtils.entityToPojo(EntityUtils.java:1014)
    at com.google.appengine.datanucleus.query.DatastoreQuery$2.apply(DatastoreQuery.java:229)
    at com.google.appengine.datanucleus.query.DatastoreQuery$2.apply(DatastoreQuery.java:226)
    at com.google.appengine.datanucleus.query.LazyResult.resolveNext(LazyResult.java:96)
    at com.google.appengine.datanucleus.query.LazyResult.resolveAll(LazyResult.java:121)
    at com.google.appengine.datanucleus.query.LazyResult.size(LazyResult.java:115)
    at com.google.appengine.datanucleus.query.StreamingQueryResult.size(StreamingQueryResult.java:151)
    at com.annkh.resources.MIResource.countPurchases(MIResource.java:67)
    at com.annkh.resources.MIResource.articlesListView(MIResource.java:60)

The entity has a simple java.util.Date field called datePurchased that seems to be causing the problem:

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;

import static javax.persistence.GenerationType.IDENTITY;

@SuppressWarnings("serial")
@Entity
public class PurchasedQuote implements Serializable {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    protected Long id;
    <blah blah other fields>

    @Basic
    protected java.util.Date datePurchased;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public java.util.Date getDatePurchased() {
        return datePurchased;
    }

    public void setDatePurchased(Date datePurchased) {
        this.datePurchased = datePurchased;
    }
}

The call that is causing the error is problems is a simple retrieve:

List<PurchasedQuote> quotes = purchaseDAO.findAll();
map.put("purchaseCount", quotes.size());

In the appengine data viewer, the date looks no different from other dates in other objects that are working just fine. The type for the field in the datastore viewer is gd:when.

I've tried tricks suggested in other answers (wrapping and unwrapping calls to get and set with a method that returns pure java.util.Date), but nothing seems to be helping. Any suggestions would be appreciated.

Was it helpful?

Solution 2

Turns out that I had forgotten a @Basic annotation on a Text field in the class. Which leads to very weird behaviour including the error shown. There was nothing wrong with the date field.

OTHER TIPS

This is not the problem with column/field datePurchased. Instead its the problem with your other fields(under <blah blah other fields> from your code) where one of your Long field is wrongfully mapped with a date type column. Just check with fields and the mapping one by one. For your information, if you do not specify the column name for a field then it will assume the field name will be the column name by default. For example:

private String testCol;
// by default it assumes the column name as testCol
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top