Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

java - Spring-Data JPA CrudRepository returns Iterable, is it OK to cast this to List?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...