This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes query.list() fails, query.iterate() doesn't Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "query.list() fails, query.iterate() doesn Watch "query.list() fails, query.iterate() doesn New topic

query.list() fails, query.iterate() doesn't

Dieter Quickfend

Joined: Aug 06, 2010
Posts: 543

So I have a simple case of two entities with a @ManyToMany implemented with a @CollectionOfElements. I do a simple query "select * from com.acme.entity.MyEntity where 1=1 order by column1 ASC, column2 ASC". This is a part of a rather generic application and works with no problems with a bunch of other entities, including the other side of the @ManyToMany, which has the same implementation of the @ElementCollection. The only real structural difference between these classes is that the ID is a String with the class where it fails, and a Long with the class where it works.

Now, I'm using JPA with vendor Hibernate, and whether I use em.getResultList or session.list, the result is the same:

After searching online and finding no answer to this problem, I noticed someone had the same issue and solved it by using "session.iterate()". I tried this and it worked without any issues.

So, my problem is solved, yes, but I'm not satisfied with that. I want to know WHY does 'list()' fail and iterate() work in this instance?

The only difference I can find between list() and iterate() is that iterate() reuses cached records whereas list() doesn't.

The entityManager is transaction scoped, the DAO is a Stateless EJB, and the method's transaction attribute is SUPPORTS.

If you need any more information, please ask. I am loath to put code on here as it doesn't really matter. I've tried many different combinations, but the em.getResultList() or session.list() always yield the same exception (above).

Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Dieter Quickfend

Joined: Aug 06, 2010
Posts: 543

For completeness' sake; this is the Collection definition in the JPA entity:

(codes are not unique)
I agree. Here's the link:
subject: query.list() fails, query.iterate() doesn't
It's not a secret anymore!