Java Spring-Data JPA CrudRepository 返回 Iterable,可以将其转换为 List 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34702252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-Data JPA CrudRepository returns Iterable, is it OK to cast this to List?
提问by James Tayler
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.
我正在编写一个代码生成工具来使用 Spring-Data-Jpa 为 Spring-boot 应用程序生成后端接线代码,这让我有点恼火,CrudRepository 中的方法返回 Iterable 而不是 List,因为 iterable 提供的不够多功能,但 List 确实如此,所以我正在寻找将可迭代对象转换为列表的最佳方法。
I saw this poston 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?
我看到这篇关于将可迭代对象更改为集合的帖子,我想知道,与其使用像 Guava 这样的库或实现我自己的函数来进行转换,为什么不将其强制转换为 List?这样做有什么我不知道的错误吗?
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?
编辑:我问,因为它是一个代码生成工具,让它生成引入对 3rd 方库的依赖的代码是不合理的,并且编写我自己的函数来进行转换也不是很合理,因为它必须存在于某个地方我宁愿在生成的代码中没有那个。一个简单的演员表会起作用,如果有点难看,但只是想知道我是否遗漏了什么?
采纳答案by Andy Wilkinson
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.
虽然 aList
保证是Iterable
anIterable
可能不是 a List
。这意味着如果您将 an 强制转换Iterable
为 aList
它可能会在运行时失败。即使它有效,也不能保证它将来会继续工作,因为它可能会在新版本的 Spring Data JPA 中发生变化而不会破坏接口的契约。
Instead of using a cast, you should declare your own query methods that return List
.
您应该声明自己的返回List
.
回答by Robert Niestroj
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.
你提到了[spring-data-jpa]
所以我猜你用的是 JPA。在这种情况下,使用JpaRepository
而不是CrudRepository
方法返回List
的位置就像您想要的那样。
回答by Biga
Your interface can still extends the CrudRepository, you can just add a new method findAll returning a list. Like example below:
您的接口仍然可以扩展CrudRepository,您只需添加一个返回列表的新方法 findAll 。像下面的例子:
@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();
}