I have the following repository:
PermissionRepository:
public interface PermissionRepository extends CrudRepository<Permission, Long>, SearchRepository<Permission> { }
In my service, I am trying to map Permission
entity to PermissionDTO
, but as findAll()
method returns Iterable<T>
(Iterable<T> findAll();
), I cannot convert as shown below as I generally use for List<T>
. It throws “Cannot resolve method ‘stream’ in ‘Iterable’” error for the .stream()
method.
List<PermissionDTO> list = permissionRepository.findAll() .stream() .map(PermissionDTO::new) .collect(Collectors.toList());
So, how can I map this Permission
entity to PermissionDTO
?
Advertisement
Answer
There are 3 ways to do what you want
Override findAll
and make it return a List
public interface PermissionRepository extends CrudRepository<Permission, Long> { @Override List<Permission> findAll(); }
Implement JpaRepository
instead of CrudRepository
.
public interface PermissionRepository extends JpaRepository<Permission, Long>, SearchRepository<Permission>
Convert Iterable
to Stream
using StreamSupport
.
StreamSupport.stream(iterable.spliterator(), false).map(PermissionDTO::new) .collect(Collectors.toList());