HHH-17948 make getAll(), findAll() accept List instead of varargs

This commit is contained in:
Gavin King 2024-09-09 11:24:46 +02:00
parent 7e36768c65
commit 62e1b0470e
8 changed files with 17 additions and 22 deletions

View File

@ -706,15 +706,12 @@ public interface Session extends SharedSessionContract, EntityManager {
* *
* @param entityType the entity type * @param entityType the entity type
* @param ids the identifiers * @param ids the identifiers
*
* @return an ordered list of persistent instances, with null elements representing missing * @return an ordered list of persistent instances, with null elements representing missing
* entities * entities
*
* @since 7.0
*
* @see #byMultipleIds(Class) * @see #byMultipleIds(Class)
* @since 7.0
*/ */
<E> List<E> findAll(Class<E> entityType, Object... ids); <E> List<E> findAll(Class<E> entityType, List<Object> ids);
/** /**
* Return the persistent instance of the given entity class with the given identifier, * Return the persistent instance of the given entity class with the given identifier,
@ -925,7 +922,7 @@ public interface Session extends SharedSessionContract, EntityManager {
* *
* @throws HibernateException If the given class does not resolve as a mapped entity * @throws HibernateException If the given class does not resolve as a mapped entity
* *
* @see #findAll(Class, Object...) * @see #findAll(Class, List)
*/ */
<T> MultiIdentifierLoadAccess<T> byMultipleIds(Class<T> entityClass); <T> MultiIdentifierLoadAccess<T> byMultipleIds(Class<T> entityClass);

View File

@ -261,13 +261,11 @@ public interface StatelessSession extends SharedSessionContract {
* *
* @param entityClass The class of the entity to retrieve * @param entityClass The class of the entity to retrieve
* @param ids The ids of the entities to retrieve * @param ids The ids of the entities to retrieve
*
* @return an ordered list of detached entity instances, with * @return an ordered list of detached entity instances, with
* null elements representing missing entities * null elements representing missing entities
*
* @since 7.0 * @since 7.0
*/ */
<T> List<T> getAll(Class<T> entityClass, Object... ids); <T> List<T> getAll(Class<T> entityClass, List<Object> ids);
/** /**
* Refresh the entity instance state from the database. * Refresh the entity instance state from the database.

View File

@ -954,7 +954,7 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
} }
@Override @Override
public <E> List<E> findAll(Class<E> entityType, Object... ids) { public <E> List<E> findAll(Class<E> entityType, List<Object> ids) {
return delegate.findAll( entityType, ids ); return delegate.findAll( entityType, ids );
} }

View File

@ -264,7 +264,7 @@ public class SessionLazyDelegator implements Session {
} }
@Override @Override
public <E> List<E> findAll(Class<E> entityType, Object... ids) { public <E> List<E> findAll(Class<E> entityType, List<Object> ids) {
return this.lazySession.get().findAll( entityType, ids ); return this.lazySession.get().findAll( entityType, ids );
} }

View File

@ -948,7 +948,7 @@ public class SessionImpl
} }
@Override @Override
public <E> List<E> findAll(Class<E> entityType, Object... ids) { public <E> List<E> findAll(Class<E> entityType, List<Object> ids) {
return this.byMultipleIds( entityType ).multiLoad( ids ); return this.byMultipleIds( entityType ).multiLoad( ids );
} }

View File

@ -513,7 +513,7 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen
} }
@Override @Override
public <T> List<T> getAll(Class<T> entityClass, Object... ids) { public <T> List<T> getAll(Class<T> entityClass, List<Object> ids) {
for (Object id : ids) { for (Object id : ids) {
if ( id == null ) { if ( id == null ) {
throw new IllegalArgumentException("Null id"); throw new IllegalArgumentException("Null id");
@ -528,7 +528,7 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen
for (T entity : resultList) { for (T entity : resultList) {
idList.add( persister.getIdentifier(entity, this) ); idList.add( persister.getIdentifier(entity, this) );
} }
final List<T> list = new ArrayList<>(ids.length); final List<T> list = new ArrayList<>( ids.size() );
for (Object id : ids) { for (Object id : ids) {
final int pos = idList.indexOf(id); final int pos = idList.indexOf(id);
list.add( pos < 0 ? null : resultList.get(pos) ); list.add( pos < 0 ? null : resultList.get(pos) );

View File

@ -21,7 +21,7 @@ public class FindAllTest {
s.persist(new Record(456L,"hello mars")); s.persist(new Record(456L,"hello mars"));
}); });
scope.inTransaction(s-> { scope.inTransaction(s-> {
List<Record> all = s.findAll(Record.class, 456L, 123L, 2L); List<Record> all = s.findAll(Record.class, List.of(456L, 123L, 2L));
assertEquals("hello mars",all.get(0).message); assertEquals("hello mars",all.get(0).message);
assertEquals("hello earth",all.get(1).message); assertEquals("hello earth",all.get(1).message);
assertNull(all.get(2)); assertNull(all.get(2));

View File

@ -21,7 +21,7 @@ public class GetAllTest {
s.insert(new Record(456L,"hello mars")); s.insert(new Record(456L,"hello mars"));
}); });
scope.inStatelessTransaction(s-> { scope.inStatelessTransaction(s-> {
List<Record> all = s.getAll(Record.class, 456L, 123L, 2L); List<Record> all = s.getAll(Record.class, List.of(456L, 123L, 2L));
assertEquals("hello mars",all.get(0).message); assertEquals("hello mars",all.get(0).message);
assertEquals("hello earth",all.get(1).message); assertEquals("hello earth",all.get(1).message);
assertNull(all.get(2)); assertNull(all.get(2));