Skip to content

Commit 7f7c861

Browse files
committed
HHH-18767 add BatchSize for use with findMultiple()
Signed-off-by: Gavin King <[email protected]>
1 parent cb0d703 commit 7f7c861

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate;
6+
7+
import jakarta.persistence.FindOption;
8+
9+
import java.util.List;
10+
11+
/**
12+
* Specify a batch size, that is, how many entities should be
13+
* fetched in each request to the database, for an invocation of
14+
* {@link Session#findMultiple(Class, List, FindOption...)}.
15+
* <ul>
16+
* <li>By default, the batch sizing strategy is determined by the
17+
* {@linkplain org.hibernate.dialect.Dialect#getBatchLoadSizingStrategy
18+
* SQL dialect}, but
19+
* <li>if some {@code batchSize>1} is specified as an
20+
* argument to this method, then that batch size will be used.
21+
* </ul>
22+
* <p>
23+
* If an explicit batch size is set manually, care should be taken
24+
* to not exceed the capabilities of the underlying database.
25+
* <p>
26+
* A batch size is considered a hint. This option has no effect
27+
* on {@link Session#find(Class, Object, FindOption...)}.
28+
*
29+
* @param batchSize The batch size
30+
*
31+
* @see Session#findMultiple
32+
* @see MultiIdentifierLoadAccess#withBatchSize
33+
*
34+
* @since 7.0
35+
*
36+
* @author Gavin King
37+
*/
38+
public record BatchSize(int batchSize) implements FindOption {
39+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ public interface Session extends SharedSessionContract, EntityManager {
561561
* Every object returned by {@code findMultiple()} is either an unproxied instance of the
562562
* given entity class, or a fully-fetched proxy object.
563563
* <p>
564+
* This method accepts {@link BatchSize} as an option, allowing control over the number of
565+
* records retrieved in a single database request.
566+
* <p>
564567
* For more advanced cases, use {@link #byMultipleIds(Class)}, which returns an instance of
565568
* {@link MultiIdentifierLoadAccess}.
566569
*

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import jakarta.persistence.PessimisticLockScope;
2626
import jakarta.persistence.Timeout;
27+
import org.hibernate.BatchSize;
2728
import org.hibernate.CacheMode;
2829
import org.hibernate.ConnectionAcquisitionMode;
2930
import org.hibernate.EntityFilterException;
@@ -950,6 +951,7 @@ private <T> MultiIdentifierLoadAccess<T> multiloadAccessWithOptions(Class<T> ent
950951
CacheStoreMode storeMode = getCacheStoreMode();
951952
CacheRetrieveMode retrieveMode = getCacheRetrieveMode();
952953
LockOptions lockOptions = copySessionLockOptions();
954+
int batchSize = -1;
953955
for ( FindOption option : options ) {
954956
if ( option instanceof CacheStoreMode cacheStoreMode ) {
955957
storeMode = cacheStoreMode;
@@ -982,8 +984,13 @@ else if ( option instanceof EnabledFetchProfile enabledFetchProfile ) {
982984
else if ( option instanceof ReadOnlyMode ) {
983985
loadAccess.withReadOnly( option == ReadOnlyMode.READ_ONLY );
984986
}
987+
else if ( option instanceof BatchSize batchSizeOption ) {
988+
batchSize = batchSizeOption.batchSize();
989+
}
985990
}
986-
loadAccess.with( lockOptions ).with( interpretCacheMode( storeMode, retrieveMode ) );
991+
loadAccess.with( lockOptions )
992+
.with( interpretCacheMode( storeMode, retrieveMode ) )
993+
.withBatchSize( batchSize );
987994
return loadAccess;
988995
}
989996

0 commit comments

Comments
 (0)