Skip to content

Commit eb5acfe

Browse files
committed
add overload of StatelessSession.getMultiple accepting a LockMode
1 parent adf7e2c commit eb5acfe

File tree

2 files changed

+80
-37
lines changed

2 files changed

+80
-37
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,23 @@ public interface StatelessSession extends SharedSessionContract {
355355
*/
356356
<T> List<T> getMultiple(Class<T> entityClass, List<?> ids);
357357

358+
/**
359+
* Retrieve multiple rows, obtaining the specified lock mode,
360+
* and returning entity instances in a list where the position
361+
* of an instance in the list matches the position of its
362+
* identifier in the given array, and the list contains a null
363+
* value if there is no persistent instance matching a given
364+
* identifier.
365+
*
366+
* @param entityClass The class of the entity to retrieve
367+
* @param ids The ids of the entities to retrieve
368+
* @param lockMode The lock mode to apply to the entities
369+
* @return an ordered list of detached entity instances, with
370+
* null elements representing missing entities
371+
* @since 7.0
372+
*/
373+
<T> List<T> getMultiple(Class<T> entityClass, List<?> ids, LockMode lockMode);
374+
358375
/**
359376
* Refresh the entity instance state from the database.
360377
*

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

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -128,42 +128,7 @@
128128
public class StatelessSessionImpl extends AbstractSharedSessionContract implements StatelessSession {
129129
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( StatelessSessionImpl.class );
130130

131-
public static final MultiIdLoadOptions MULTI_ID_LOAD_OPTIONS = new MultiIdLoadOptions() {
132-
@Override
133-
public boolean isSessionCheckingEnabled() {
134-
return false;
135-
}
136-
137-
@Override
138-
public boolean isSecondLevelCacheCheckingEnabled() {
139-
return true;
140-
}
141-
142-
@Override
143-
public Boolean getReadOnly(SessionImplementor session) {
144-
return null;
145-
}
146-
147-
@Override
148-
public boolean isReturnOfDeletedEntitiesEnabled() {
149-
return false;
150-
}
151-
152-
@Override
153-
public boolean isOrderReturnEnabled() {
154-
return true;
155-
}
156-
157-
@Override
158-
public LockOptions getLockOptions() {
159-
return null;
160-
}
161-
162-
@Override
163-
public Integer getBatchSize() {
164-
return null;
165-
}
166-
};
131+
public static final MultiIdLoadOptions MULTI_ID_LOAD_OPTIONS = new MultiLoadOptions();
167132

168133
private final LoadQueryInfluencers influencers;
169134
private final PersistenceContext temporaryPersistenceContext;
@@ -802,9 +767,24 @@ public <T> T get(
802767
}
803768
}
804769

770+
@Override
771+
public <T> List<T> getMultiple(Class<T> entityClass, List<?> ids, LockMode lockMode) {
772+
for ( Object id : ids ) {
773+
if ( id == null ) {
774+
throw new IllegalArgumentException( "Null id" );
775+
}
776+
}
777+
778+
final EntityPersister persister = requireEntityPersister( entityClass.getName() );
779+
780+
final List<?> results = persister.multiLoad( ids.toArray(), this, new MultiLoadOptions(lockMode) );
781+
//noinspection unchecked
782+
return (List<T>) results;
783+
}
784+
805785
@Override
806786
public <T> List<T> getMultiple(Class<T> entityClass, List<?> ids) {
807-
for (Object id : ids) {
787+
for ( Object id : ids ) {
808788
if ( id == null ) {
809789
throw new IllegalArgumentException("Null id");
810790
}
@@ -1407,4 +1387,50 @@ public Object loadFromSecondLevelCache(EntityPersister persister, EntityKey enti
14071387
return CacheLoadHelper.loadFromSecondLevelCache( this, instanceToLoad, lockMode, persister, entityKey );
14081388
}
14091389

1390+
private static final class MultiLoadOptions implements MultiIdLoadOptions {
1391+
private final LockOptions lockOptions;
1392+
1393+
private MultiLoadOptions() {
1394+
this.lockOptions = null;
1395+
}
1396+
1397+
private MultiLoadOptions(LockMode lockOptions) {
1398+
this.lockOptions = new LockOptions( lockOptions );
1399+
}
1400+
1401+
@Override
1402+
public boolean isSessionCheckingEnabled() {
1403+
return false;
1404+
}
1405+
1406+
@Override
1407+
public boolean isSecondLevelCacheCheckingEnabled() {
1408+
return true;
1409+
}
1410+
1411+
@Override
1412+
public Boolean getReadOnly(SessionImplementor session) {
1413+
return null;
1414+
}
1415+
1416+
@Override
1417+
public boolean isReturnOfDeletedEntitiesEnabled() {
1418+
return false;
1419+
}
1420+
1421+
@Override
1422+
public boolean isOrderReturnEnabled() {
1423+
return true;
1424+
}
1425+
1426+
@Override
1427+
public LockOptions getLockOptions() {
1428+
return lockOptions;
1429+
}
1430+
1431+
@Override
1432+
public Integer getBatchSize() {
1433+
return null;
1434+
}
1435+
}
14101436
}

0 commit comments

Comments
 (0)