java - org.hibernate.ObjectNotFoundException at query iterating -


i'm trying localize bug in following code snippet:

query booklogs = session.createquery("from booklog log"); (iterator = booklogs.iterate(); it.hasnext();) {   booklog booklog = (booklog) it.next();   bookno = booklog.getno();   ...   session.delete(booklog);   session.flush(); } 

following exception occurs infrequently @ booklog.getno():

org.hibernate.objectnotfoundexception: no row given identifier exists: [de.store.books.booklog#2343] 

i suppose conflict @ deleting db entry while iterator not adjusted calling it.remove().

booklog not reference tables , mapping accordingly simple.

as query.interate() returns proxy iterator, id of entities fetch.

executes 1+n sql queries. first query returns identifier of records , when returned iterator iterated each time separate sql query executed contains clause id=n. if records present in cache first query executed , rest n queries not executed , records obtained cache.

so concurrent modification problem. while execute query

for (iterator = booklogs.iterate(); it.hasnext();) { 

given booklog exist , id returned iterator. while iterating, every

 bookno = booklog.getno() 

separate select query performed, on point, whole table has been modified concurrent action. row given id not exist anymore, , exception thrown. avoid have use either proper transaction-isolation or use query.getresultlist(). return results in single query. however, there no guarantee (again) when try delete returned entity, still in database.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -