Skip to content

Commit 833574a

Browse files
committed
overload findMultiple() to accept EntityGraph
1 parent abdffb4 commit 833574a

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,46 @@ public interface Session extends SharedSessionContract, EntityManager {
597597
*/
598598
<E> List<E> findMultiple(Class<E> entityType, List<?> ids, FindOption... options);
599599

600+
/**
601+
* Return the persistent instances of the root entity of the given {@link EntityGraph}
602+
* with the given identifiers as a list, fetching the associations specified by the
603+
* graph, which is interpreted as a {@linkplain org.hibernate.graph.GraphSemantic#LOAD
604+
* load graph}. The position of an instance in the returned list matches the position of
605+
* its identifier in the given list of identifiers, and the returned list contains a null
606+
* value if there is no persistent instance matching a given identifier. If an instance
607+
* is already associated with the session, that instance is returned. This method never
608+
* returns an uninitialized instance.
609+
* <p>
610+
* Every object returned by {@code findMultiple()} is either an unproxied instance of the
611+
* given entity class, or a fully-fetched proxy object.
612+
* <p>
613+
* This method accepts {@link BatchSize} as an option, allowing control over the number of
614+
* records retrieved in a single database request. The performance impact of setting a batch
615+
* size depends on whether a SQL array may be used to pass the list of identifiers to the
616+
* database:
617+
* <ul>
618+
* <li>for databases which {@linkplain org.hibernate.dialect.Dialect#supportsStandardArrays
619+
* support standard SQL arrays}, a smaller batch size might be extremely inefficient
620+
* compared to a very large batch size or no batching at all, but
621+
* <li>on the other hand, for databases with no SQL array type, a large batch size results
622+
* in long SQL statements with many JDBC parameters.
623+
* </ul>
624+
* <p>
625+
* For more advanced cases, use {@link #byMultipleIds(Class)}, which returns an instance of
626+
* {@link MultiIdentifierLoadAccess}.
627+
*
628+
* @param entityGraph the entity graph interpreted as a load graph
629+
* @param ids the list of identifiers
630+
* @param options options, if any
631+
*
632+
* @return an ordered list of persistent instances, with null elements representing missing
633+
* entities, whose positions in the list match the positions of their ids in the
634+
* given list of identifiers
635+
* @see #byMultipleIds(Class)
636+
* @since 7.0
637+
*/
638+
<E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> ids, FindOption... options);
639+
600640
/**
601641
* Read the persistent state associated with the given identifier into the given
602642
* transient instance.

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,11 @@ public <E> List<E> findMultiple(Class<E> entityType, List<?> ids, FindOption...
967967
return delegate.findMultiple( entityType, ids, options );
968968
}
969969

970+
@Override
971+
public <E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> ids, FindOption... options) {
972+
return delegate.findMultiple( entityGraph, ids, options );
973+
}
974+
970975
@Override
971976
public <T> T get(Class<T> theClass, Object id) {
972977
return delegate.get( theClass, id );

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ public <E> List<E> findMultiple(Class<E> entityType, List<?> ids, FindOption...
271271
return this.lazySession.get().findMultiple( entityType, ids, options );
272272
}
273273

274+
@Override
275+
public <E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> ids, FindOption... options) {
276+
return this.lazySession.get().findMultiple( entityGraph, ids, options );
277+
}
278+
274279
@Override
275280
public <T> T get(Class<T> entityType, Object id) {
276281
return this.lazySession.get().get( entityType, id );

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,16 @@ public <E> List<E> findMultiple(Class<E> entityType, List<?> ids, FindOption...
10511051
return loadAccess.multiLoad( ids );
10521052
}
10531053

1054+
@Override
1055+
public <E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> ids, FindOption... options) {
1056+
final RootGraph<E> rootGraph = (RootGraph<E>) entityGraph;
1057+
final MultiIdentifierLoadAccess<E> loadAccess =
1058+
byMultipleIds( rootGraph.getGraphedType().getJavaType() );
1059+
loadAccess.withLoadGraph( rootGraph );
1060+
setMultiIdentifierLoadAccessOptions( options, loadAccess );
1061+
return loadAccess.multiLoad( ids );
1062+
}
1063+
10541064
@Override
10551065
public <T> T get(Class<T> entityClass, Object id) {
10561066
return byId( entityClass ).load( id );

0 commit comments

Comments
 (0)