1

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?

  • Why not? A `List` is an `Iterable`. – Robby Cornelissen Oct 25 '21 at 08:30
  • I guess you are using Spring Data? How about using `JpaRepository` instead of `CrudRepository`. `findAll` of `JpaRepository` returns a `List`, – magicmn Oct 25 '21 at 08:33
  • 1
    [Convert Iterable to Stream using Java 8 JDK](https://stackoverflow.com/questions/23932061/convert-iterable-to-stream-using-java-8-jdk) -> that's probably what you're looking for. – ernest_k Oct 25 '21 at 08:34
  • 1
    @RobbyCornelissen It throws "*Cannot resolve method 'stream' in 'Iterable'*" error for the `.stream()` method. –  Oct 25 '21 at 08:38
  • @magicmn Yes, it seems possible but at first I do not want to change that part if there is another smart solution for this. –  Oct 25 '21 at 08:43
  • @Jonathan Gotcha. Misread the question. – Robby Cornelissen Oct 25 '21 at 08:43
  • Does this answer your question? [Convert Iterable to Stream using Java 8 JDK](https://stackoverflow.com/questions/23932061/convert-iterable-to-stream-using-java-8-jdk) – Maurice Perry Oct 25 '21 at 08:47
  • @ernest_k It does not seem a proper solution I think. –  Oct 25 '21 at 08:49
  • @MauricePerry I tried to use it, but cannot. How should I apply that approach to my code above? –  Oct 25 '21 at 08:51

1 Answers1

0

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());
magicmn
  • 1,787
  • 7
  • 15
  • Perfect explanations and good answer. I think it will fix the problem, but in all of these approaches, I get a warning "*Required type: Function, Provided: *". What is the mistake? –  Oct 25 '21 at 08:58
  • Hm a random guess is that `PermissionDTO::new` is not working as expected. Your PermissionDTO has a PermissionDTO(Permission) constructor right? – magicmn Oct 25 '21 at 09:03
  • Is there any problem with `.map(PermissionDTO::new)` line? –  Oct 25 '21 at 09:04
  • 1
    I think so `.map(PermissionDTO::new)` is a shortcut for `.map(permission -> new PermissionDTO(permission))`. Is that working? – magicmn Oct 25 '21 at 09:05
  • Adding the necessary constructor fixed the problem, thanks a lot. –  Oct 25 '21 at 09:07