Back Forum Reply New

weird behaviour

Hello guys

i am wotrking in a second proyect and i am re using code from the first project, well i have
and used to work by default with this style block of code

Code:
Listltagogt; mylist = new ArrayListltagogt;();
Query query = null;

try{
query = this.getSession().createQuery(
quot; FROM Pago p WHERE p.fecha BETWEEN :date1 AND :date2 quot;+
quot; ORDER BY p.fecha DESC quot;);

query.setParameter(quot;date1quot;, date1);
query.setParameter(quot;date2quot;, date2);
query.setFirstResult( numrows *(page-1));
query.setMaxResults(numrows);
mylist = query.list();

if(mylist.isEmpty())
throw new MyListEmptyException();

logger.info(quot;\nsize list: quot;+mylist.size()+quot;\nquot;);

Iterator it = mylist.iterator();

while(it.hasNext()){
//logger.info(quot;\n\nquot;);
logger.info(quot;\n One \nquot;);
Pago pago = (Pago) it.next();

getHibernateTemplate().initialize(pago);

..... .....    mylist.add(pago);
}
in my first project (remember style of work, not the same code)
works fine

but here (code shown above) throws this exception

Code:
Hibernate: select pago0_.idPago as idPago12_, pago0_.estado as estado12_, pago0_.fecha as fecha12_, pago0_.concepto as concepto12_, pago0_.observaciones as observac5_12_, pago0_.idPagoCuentaProveedores as idPagoCu6_12_, pago0_.idCabeceraFacturaCompra as idCabece7_12_, pago0_.idCaja as idCaja12_, pago0_.idCheque as idCheque12_ from Pago pago0_ where pago0_.fecha between ? and ? order by pago0_.fecha DESC limit ?
13:10:12 INFO         com.panamotor.modelo.dao.implementaciones.PagoDAOImpl RangosFechasPaginacionDAO
size list: 1

13:10:12 INFO         com.panamotor.modelo.dao.implementaciones.PagoDAOImpl RangosFechasPaginacionDAO One 13:10:12 INFO         com.panamotor.modelo.dao.implementaciones.PagoDAOImpl RangosFechasPaginacionDAO One

13:10:12 ERROR ontainerBase.[Catalina].[localhost].[/panamotor].[panamotor] invoke                    Servlet.service() para servlet panamotor lanzó excepción
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
at java.util.AbstractList$Itr.next(AbstractList.java:420)
at com.panamotor.modelo.dao.implementaciones.PagoDAOImpl.getAllPagosByRangosFechasPaginacionDAO(Unknown Source)
at com.panamotor.modelo.bo.implementaciones.abstractos.AbstractPagoBO.doGetAllPagosByRangosFechasPaginacionBO(Unknown Source)
at com.panamotor.modelo.bo.implementaciones.concretos.ConcretePagoImpl.getAllPagosByRangosFechasPaginacionBO(Unknown Source)
at com.panamotor.modelo.bo.implementaciones.concretos.ConcretePagoImpl$$FastClassByCGLIB$$9bcdcf22.invoke(lt;generatedgt;)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
see carefully that log4j show that the list size value is size list: 1 ok?
and in my while code, log4j must show quot;Onequot; text for each iteration

i cant understand why appear twice quot;Onequot; (only must appear once) in my stacktrace and then appear the exception??rememeber that in my first project my logic of use the list of the Query object for a normal List
like mylist = query.list();  works normal or fine

i only can resolve this problem if i change my code to this way

Code:
try{
query = this.getSession().createQuery(
quot; FROM Pago p WHERE p.fecha BETWEEN :date1 AND :date2 quot;+
quot; ORDER BY p.fecha DESC quot;);

query.setParameter(quot;date1quot;, date1);
query.setParameter(quot;date2quot;, date2);
query.setFirstResult( numrows *(page-1));
query.setMaxResults(numrows);

//mylist = query.list(); // Godd Bye

if(query.list().isEmpty())
throw new MyListEmptyException();

logger.info(quot;\nsize list: quot;+query.list().size()+quot;\nquot;);

Iterator it = query.iterate();

while(it.hasNext()){      //logger.info(quot;\n\nquot;);
logger.info(quot;\n One \nquot;);
Pago pago = (Pago) it.next();   ... .... ...
mylist.add(pago);
}
}
well the code shown above, works fine,

i cant understand why the second way works instead of the first ,
it should has the same behaviour???

more confused yet, my first project works with both ways already written and first time that i recieve this weird exception for the first way for this second project

more funny yet
in the second way if i work

Code:
Iterator it = query.iterate();
or

Code:
Iterator it = query.list().iterator();
works normal

is it the same right?

Code:
Iterator it = query.list().iterator();
against

Code:
mylist = query.list();
Iterator it = mylist.iterator();
the only difference is the variable mylist
thanks for advanced

From what I understand the difference is between

Code:
Iterator it = query.iterate();
and

Code:
Iterator it = query.list().iterator();
I don't believe that it is between the latter and assigning query.list() to myList and calling iterator() on it - there is NO difference.

Anyway, you are not supposed to modify the list while operating with an iterator of it. That's what the exception is saying. I also guess query.iterate() gives you an iterator object independent from the list returned by query.list().

The correct solution is to retrieve a ListIterator from the ArrayList which allows you to call add() on the iterator object instead of the list!

Joerg
¥
Back Forum Reply New