52

I'm writing a code-gen tool to generate backend wiring code for Spring Boot applications using Spring Data JPA and it's mildly annoying me that the methods in the CrudRepository return Iterable rather than List, as Iterable doesn't provide quite enough functionality, but List does, so I'm looking for the best way to convert the Iterable into a List.

I saw this post on changing an Iterable to a Collection and I was wondering, rather than using a library like Guava or implementing my own function to do the conversion, why not just cast it to List? Is there something wrong with doing that that I don't know about?

Edit: I ask because since it's a code-gen tool it's not reasonable to make it generate code that introduces dependencies on 3rd party libraries, and writing my own function to do the conversion also isn't really reasonable because it would have to live somewhere and I'd rather not have that in the generated code. A simple cast will work, if a little ugly, but just wondered if there's something I'm missing?

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
james_s_tayler
  • 1,823
  • 1
  • 15
  • 20

6 Answers6

135

You mentioned [spring-data-jpa] so i guess you use JPA. In this case use JpaRepository instead of CrudRepository where the methods return List's like you want it.

Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119
  • 9
    Cool. I just took a look at the documentation now. JpaRepository extends PagingAndSortingRepository which extends CrudRepository which extends Repository. So it kinda builds on the others. Neat. Yeah, I think the cleanest solution is to switch to JpaRepository. – james_s_tayler Jan 10 '16 at 09:58
  • 3
    this unfortunatelly does not work always. -> for example when you're using a `PagingAndSortingRepository` it is inherited from the `CrudRepository`. – eav Mar 16 '17 at 14:36
66

No, I don't think it's OK.

While a List is guaranteed to be an Iterable an Iterable may not be a List. This means that if you do cast an Iterable to a List it may fail at runtime. Even if it works, there's no guarantee that it will continue to work in the future as it could change in new versions of Spring Data JPA without breaking the interface's contract.

Instead of using a cast, you should declare your own query methods that return List.

Alternatively you may use Streamable.of(iterable).toList() for doing the conversion. This answer also contains some background why Iterable was chosen as the return type for these methods.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
52

Your interface can still extends the CrudRepository, you can just add a new method findAll returning a list. Like example below:

@Repository
public interface DataRepository extends CrudRepository<Data, Long> {

    @Override
    List<Data> findAll();

}

If you have an "abstract" repository to be extended by all your repositories, you can add this method too, so it will has effect to all your repositories. Like example below:

@NoRepositoryBean
public interface GenericRepository<T> extends CrudRepository<T, Long> {

    @Override
    List<T> findAll();

}
Biga
  • 621
  • 5
  • 7
  • 1
    Why we don't need to actually implement "List findAll()" – Shade Jan 14 '21 at 10:55
  • 2
    Hi Shade.... Spring Data knows how to implement this. We explain what to do using the method declaration and Spring Data do the implementation. Like query methods with filters for example. See https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods – Biga Feb 18 '21 at 21:12
  • @Biga But why when I would like to override `findById` to not return the Optional, I get error `The return type is incompatible with CrudRepository.findById(String)`? – Newbie Jun 14 '23 at 09:02
2

Starting from Spring Data 3 there is another option: ListCrudRepository.

This blog post covers why CrudRepository chose Iterable instead of List.

https://spring.io/blog/2022/02/22/announcing-listcrudrepository-friends-for-spring-data-3-0

Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119
0

Hi I know I am late to the party here but if you want to convert an Iterable to a Collection you can do it this way.

private <T> Collection<T> map(final Iterable<T> iterable) {
        return StreamSupport.stream(iterable.spliterator(), false)
                            .collect(Collectors.toCollection(HashSet::new));
    }

After that, du can get a List by .stream().toList(); on the Collection. Hope it helps

Wheelchair Geek
  • 364
  • 2
  • 12
0

Simply, you can make your repository to extend JpaRepository not CrudRepository. This will work perfectly. Here findAll returns a List not an Iterable.

Christian
  • 61
  • 1
  • 2