Skip to content

Commit 8d667e2

Browse files
committed
overload getMultiple() to accept EntityGraph
1 parent 27c0e8b commit 8d667e2

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

hibernate-core/src/main/java/org/hibernate/StatelessSession.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,45 @@ public interface StatelessSession extends SharedSessionContract {
372372
*/
373373
<T> List<T> getMultiple(Class<T> entityClass, List<?> ids, LockMode lockMode);
374374

375+
/**
376+
* Retrieve multiple rows, returning instances of the root
377+
* entity of the given {@link EntityGraph} with the fetched
378+
* associations specified by the graph, in a list where the
379+
* position of an instance in the list matches the position
380+
* of its identifier in the given array, and the list
381+
* contains a null value if there is no persistent instance
382+
* matching a given identifier.
383+
*
384+
* @param entityGraph The {@link EntityGraph}, interpreted as a
385+
* {@linkplain org.hibernate.graph.GraphSemantic#LOAD load graph}
386+
* @param graphSemantic a {@link GraphSemantic} specifying
387+
* how the graph should be interpreted
388+
* @param ids The ids of the entities to retrieve
389+
* @return an ordered list of detached entity instances, with
390+
* null elements representing missing entities
391+
* @since 7.0
392+
*/
393+
<T> List<T> getMultiple(EntityGraph<T> entityGraph, List<?> ids);
394+
395+
/**
396+
* Retrieve multiple rows, returning instances of the root
397+
* entity of the given {@link EntityGraph} with the fetched
398+
* associations specified by the graph, in a list where the
399+
* position of an instance in the list matches the position
400+
* of its identifier in the given array, and the list
401+
* contains a null value if there is no persistent instance
402+
* matching a given identifier.
403+
*
404+
* @param entityGraph The {@link EntityGraph}
405+
* @param graphSemantic a {@link GraphSemantic} specifying
406+
* how the graph should be interpreted
407+
* @param ids The ids of the entities to retrieve
408+
* @return an ordered list of detached entity instances, with
409+
* null elements representing missing entities
410+
* @since 7.0
411+
*/
412+
<T> List<T> getMultiple(EntityGraph<T> entityGraph, GraphSemantic graphSemantic, List<?> ids);
413+
375414
/**
376415
* Refresh the entity instance state from the database.
377416
*

hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,39 @@ public <T> List<T> getMultiple(Class<T> entityClass, List<?> ids, LockMode lockM
801801
}
802802

803803
final EntityPersister persister = requireEntityPersister( entityClass.getName() );
804-
805804
final List<?> results = persister.multiLoad( ids.toArray(), this, new MultiLoadOptions(lockMode) );
806805
//noinspection unchecked
807806
return (List<T>) results;
808807
}
808+
@Override
809+
public <T> List<T> getMultiple(EntityGraph<T> entityGraph, List<?> ids) {
810+
return getMultiple( entityGraph, GraphSemantic.LOAD, ids );
811+
}
812+
813+
@Override
814+
public <T> List<T> getMultiple(EntityGraph<T> entityGraph, GraphSemantic graphSemantic, List<?> ids) {
815+
for ( Object id : ids ) {
816+
if ( id == null ) {
817+
throw new IllegalArgumentException( "Null id" );
818+
}
819+
}
820+
821+
final RootGraphImplementor<T> rootGraph = (RootGraphImplementor<T>) entityGraph;
822+
823+
final EffectiveEntityGraph effectiveEntityGraph =
824+
getLoadQueryInfluencers().getEffectiveEntityGraph();
825+
effectiveEntityGraph.applyGraph( rootGraph, graphSemantic );
826+
827+
try {
828+
final EntityPersister persister = requireEntityPersister( rootGraph.getGraphedType().getTypeName() );
829+
final List<?> results = persister.multiLoad( ids.toArray(), this, MULTI_ID_LOAD_OPTIONS );
830+
//noinspection unchecked
831+
return (List<T>) results;
832+
}
833+
finally {
834+
effectiveEntityGraph.clear();
835+
}
836+
}
809837

810838
@Override
811839
public <T> List<T> getMultiple(Class<T> entityClass, List<?> ids) {
@@ -816,7 +844,6 @@ public <T> List<T> getMultiple(Class<T> entityClass, List<?> ids) {
816844
}
817845

818846
final EntityPersister persister = requireEntityPersister( entityClass.getName() );
819-
820847
final List<?> results = persister.multiLoad( ids.toArray(), this, MULTI_ID_LOAD_OPTIONS );
821848
//noinspection unchecked
822849
return (List<T>) results;

0 commit comments

Comments
 (0)