You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I propose the spec add the capability to load multiple entities by id at one time (multi-load).
Many use cases require loading multiple entities at once. Querying could definitely be an option.
EntityManager em = ...;
List<Product> products = em.createQuery( "select p from Product p where p.id in (:ids)", Product.class )
.setParameter( "ids", Arrays.asList( 1, 2, 3, 4 )
.getResultList();
However, that has a few drawbacks - the main one being potentially returning way too much data from the database when many ids are requested and a significant number of them are already managed in the EntityManager.
To really get this full capability with JPA today, a user would need to perform a complex series of calls. E.g.
EntityManager em = ...;
EntityManagerFactory emf = em.getFactory();
int[] ids = new int[] { 1, 2, 3, 4 }
List<Product> products = new ArrayList<>();
for ( Integer id : ids ) {
// see if it already exists...
// NOTE : overly simplistic...
Product existing = em.getReference( Product.class, id );
if ( emf.getPersistenceUnitUtil().isLoaded( existing ) ) {
products.add( existing );
}
else {
// we could get fancy here and collect the ids to load from db and issue one query,
// but lets do the simple thing here for illustration purposes
products.add( initializeProxy( product ) );
}
}
One option for API in JPA would be overloads of the existing #find methods (assuming we don't want multiple #getReference loading):
I propose the spec add the capability to load multiple entities by id at one time (multi-load).
Many use cases require loading multiple entities at once. Querying could definitely be an option.
However, that has a few drawbacks - the main one being potentially returning way too much data from the database when many ids are requested and a significant number of them are already managed in the EntityManager.
To really get this full capability with JPA today, a user would need to perform a complex series of calls. E.g.
One option for API in JPA would be overloads of the existing
#find
methods (assuming we don't want multiple#getReference
loading):Possibly accepting List of ids in addition to or instead of:
The text was updated successfully, but these errors were encountered: