File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes How to retrieve all entities when I have a list of with all required objects PKs? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "How to retrieve all entities when I have a list of with all required objects PKs?" Watch "How to retrieve all entities when I have a list of with all required objects PKs?" New topic
Author

How to retrieve all entities when I have a list of with all required objects PKs?

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 550
Hi,

I am using JPA and I have a list object containing all PKs of the objects I required to retrieve from database. What is the best approach to use this list in order to acquire the objects themselves?

Thank you in advance.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 550
If you think most of the objects are already in the JPA cache, then just use find().

Otherwise, using an IN query with the list of Ids is probably the most efficient (assuming singleton Id anyway).
If the list of Ids is very large, then you may need to split it up into batches of 200.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 550
Thank you for posting reply.
Can you please explain more about using IN? should I create a JPQL dynamically and include all PK form the list into the dynamically created query or I can pass the list to JPQL directly?


James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 550
As of JPA 2.0 you can use a list parameter with an IN. Some JPA providers supported this in JPA 1.0 as well.

i.e.
"Select e from Employee e where e.id IN :ids"
raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 550
Thank you.

My problem solved.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to retrieve all entities when I have a list of with all required objects PKs?
 
Similar Threads
Util2
How to retained the object on page submit from jsf
Alternating between Eager and Lazy Fetching
Struts2 polymorphic list in select tag
request object and JSTL