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

@ -705,16 +705,13 @@ public interface Session extends SharedSessionContract, EntityManager {
* {@link MultiIdentifierLoadAccess}.
*
* @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
* entities
*
* @since 7.0
*
* entities
* @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,
@ -925,7 +922,7 @@ public interface Session extends SharedSessionContract, EntityManager {
*
* @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);

View File

@ -260,14 +260,12 @@ public interface StatelessSession extends SharedSessionContract {
* instance matching a given identifier.
*
* @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
* null elements representing missing entities
*
* null elements representing missing entities
* @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.

View File

@ -954,7 +954,7 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
}
@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 );
}

View File

@ -264,7 +264,7 @@ public class SessionLazyDelegator implements Session {
}
@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 );
}

View File

@ -948,7 +948,7 @@ public class SessionImpl
}
@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 );
}

View File

@ -513,7 +513,7 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen
}
@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) {
if ( id == null ) {
throw new IllegalArgumentException("Null id");
@ -522,13 +522,13 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen
final EntityPersister persister = getEntityPersister( entityClass.getName() );
final JpaCriteriaQuery<T> query = getCriteriaBuilder().createQuery(entityClass);
final JpaRoot<T> from = query.from(entityClass);
query.where( from.get(persister.getIdentifierPropertyName()).in(ids) );
query.where( from.get( persister.getIdentifierPropertyName() ).in(ids) );
final List<T> resultList = createSelectionQuery(query).getResultList();
final List<Object> idList = new ArrayList<>(resultList.size());
final List<Object> idList = new ArrayList<>( resultList.size() );
for (T entity : resultList) {
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) {
final int pos = idList.indexOf(id);
list.add( pos < 0 ? null : resultList.get(pos) );

View File

@ -21,7 +21,7 @@ public class FindAllTest {
s.persist(new Record(456L,"hello mars"));
});
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 earth",all.get(1).message);
assertNull(all.get(2));

View File

@ -21,7 +21,7 @@ public class GetAllTest {
s.insert(new Record(456L,"hello mars"));
});
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 earth",all.get(1).message);
assertNull(all.get(2));