You are handling with large result sets and need to fetch all results anyway? Remember JPA holds a pointer to your database with each request. So for instance this foreach will go back to the DB every 10 (as this is the standard-value in Websphere with openJPA) items to get the next batch of items. But as a call to the DB is costly, you should avoid that. This can be easily done by adjusting the size of each batch: FetchBatchSize Be warned, that casting to an OpenJPAQuery sets other parameters as default: Like setting the FetchMode to Parallel, which is not always a good idea: Especially if you have m:n relations which must be eager-loaded, as they are needed anyway, it’s recommended to set the FetchMode to join. These query-based settings can be also done in your persistence.xml for all your queries handled by the according persistence-unit. If you have other simple count or search operations to do on your objects, see the @LRS Annotation of OpenJPA:

Leave a Reply