views:

1887

answers:

3

I need to iterate a List<myClass> in a jsp. This is how I obtain the list:

(when I commented it, the page loaded just fine).

 <%
      List<myClass> pjList = null;
      StringBuffer ejbQuery = new StringBuffer();

      EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistence");
      EntityManager em = emf.createEntityManager();

      ejbQuery.append("SELECT e ");
      ejbQuery.append("FROM myClass e ");
      pjList = em.createQuery(ejbQuery.toString()).getResultList();


for(myClass pj : pjList)
{
    %>
    <br />
    <%= pj.getSomeField()%>
    <br />
    <%
}
%>

This is the error I get when running it in google appengine. locally it runs fine.

Uncaught exception from servlet
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
 at org.datanucleus.store.appengine.DatastoreFieldManager.fetchStringField(DatastoreFieldManager.java:188)
 at org.datanucleus.state.AbstractStateManager.replacingStringField(AbstractStateManager.java:1180)
 at ar.edu.kennedy.proveedores.entities.ProEnteEy.jdoReplaceField(ProEnteEy.java)
 at ar.edu.kennedy.proveedores.entities.ProPersonaJuridicaEy.jdoReplaceField(ProPersonaJuridicaEy.java)
 at ar.edu.kennedy.proveedores.entities.ProEnteEy.jdoReplaceFields(ProEnteEy.java)
 at org.datanucleus.state.JDOStateManagerImpl.replaceFields(JDOStateManagerImpl.java:2772)
 at org.datanucleus.state.JDOStateManagerImpl.replaceFields(JDOStateManagerImpl.java:2791)
 at org.datanucleus.store.appengine.DatastorePersistenceHandler.fetchObject(DatastorePersistenceHandler.java:443)
 at org.datanucleus.store.appengine.query.DatastoreQuery.entityToPojo(DatastoreQuery.java:433)
 at org.datanucleus.store.appengine.query.DatastoreQuery.entityToPojo(DatastoreQuery.java:391)
 at org.datanucleus.store.appengine.query.DatastoreQuery.access$800(DatastoreQuery.java:97)
 at org.datanucleus.store.appengine.query.DatastoreQuery$5.apply(DatastoreQuery.java:515)
 at org.datanucleus.store.appengine.query.DatastoreQuery$5.apply(DatastoreQuery.java:507)
 at org.datanucleus.store.appengine.query.StreamingQueryResult.resolveNext(StreamingQueryResult.java:137)
 at org.datanucleus.store.appengine.query.StreamingQueryResult$1.computeNext(StreamingQueryResult.java:163)
 at org.datanucleus.store.appengine.query.AbstractIterator.tryToComputeNext(AbstractIterator.java:132)
 at org.datanucleus.store.appengine.query.AbstractIterator.hasNext(AbstractIterator.java:127)
 at org.datanucleus.store.appengine.query.StreamingQueryResult$AbstractListIterator.hasNext(StreamingQueryResult.java:229)
 at org.apache.jsp.busqueda_jsp._jspService(busqueda_jsp.java:138)

If I use a ListIterator and call hasNext() I get the same error. Help me understand what is happening, how to solve this?

A: 

I can only guess that myClass doesn't match its description (probably something XML-based ;-) in the persistence layer.

ammoQ
+1  A: 

The problem is that in your class myClass has a field of type String that is representing by an integer in the database. The DataNucleus JDO is trying to convert this value into a Long, which is not a String, thus the error. You need to make sure the datatypes of the object match the data in the data store.

Rob Di Marco
Note that I don't have a database, just classes that are persistent. Can a mismatch exist in that case?
rec
As Oscar Reyes, it appears the underlying implementation is using the JDO features.
Rob Di Marco
+1  A: 

It looks like the mapping you have from "myclass" is wrong.

There is a field marked as "String" when in fact it is a "number".

What it seems to happen from the StackTrace is that your value is fetched from the database and then casted to a string.

Since the value is not an string, you get that exception.

Try identifying in your class which "numeric" values are mapped as String and have them fixed. Start one by one until it works.

OscarRyz
Note that I don't have a database, just classes that are persistent. Can a mismatch exist in that case?
rec
You may not have a database managed by your self and although I'm not familiar with datanucleus there is a big chance they use one internally. Even if they don't use a db as persistance, they will be "marshalling" and "unmarshalling" the data and they will probably use a mapping to know how to do that. Your problem seems to come in that "map"
OscarRyz
yes, old data was still stored with a previous field type, the query returned old data and new data (!). due to changes in the class members type, deleting the old data fixed the problem.
rec