[英]spring data 3.0 PagingAndSortingRepository, migrating from 2.0
This might be a stupid question to any java knowledged person, but me just looking at from logical point of it doesn't seem to be that simple.对于任何 java 知识渊博的人来说,这可能是一个愚蠢的问题,但我只是从逻辑的角度来看它似乎并不那么简单。
Previously when PagingAndSortingRepository extended CrudRepository in spring data 2.0, we had this piece of code which worked just fine以前当 PagingAndSortingRepository 在 spring 数据 2.0 中扩展 CrudRepository 时,我们有这段代码工作得很好
public abstract class AbstractBaseServiceImpl<R extends PagingAndSortingRepository<E, K>, E extends AbstractBaseEntity, K extends Serializable, M> implements AbstractBaseService<E, K, M> {
protected Logger logger = LoggerFactory.getLogger(getClass());
protected R repository;
Now when trying to use spring boot 3, this for example won't work现在当尝试使用 spring 引导 3 时,例如这将不起作用
E entity = repository.findById(id).orElse(null);
Because PagingAndSortingRepository doesn't have findById anymore, since it's inherited from CrudRepository.因为 PagingAndSortingRepository 没有 findById 了,因为它继承自 CrudRepository。 I kinda found the solution to this, https://spring.io/blog/2022/02/22/announcing-listcrudrepository-friends-for-spring-data-3-0 , but can't figure out how to implement the fix here.我有点找到解决方案https://spring.io/blog/2022/02/22/announcing-listcrudrepository-friends-for-spring-data-3-0 ,但无法弄清楚如何实现修复这里。 Any syntax I tried doesn't work.我尝试过的任何语法都不起作用。
Tried this, it doesn't seem to be right试过了,好像不对
public abstract class AbstractBaseServiceImpl<R extends PagingAndSortingRepository<E, K>, CrudRepository<E, K>, E extends AbstractBaseEntity, K extends Serializable, M> implements AbstractBaseService<E, K, M> {
So, hopefully I'll get a quick fix from here and maybe learn something on the way:)所以,希望我能从这里得到一个快速修复,并可能在途中学到一些东西:)
I think you should get this to work by declaring R
like this:我认为你应该像这样声明R
来让它工作:
<R extends CrudRepositor<E, K> & PagingAndSortingRepository<E, K>, …>
You can see a graphical representation of the difference between spring boot 2 and spring boot 3 under this previous answer您可以在之前的答案下看到 spring boot 2 和 spring boot 3 之间差异的图形表示
As to how to fix your code during this migration you should change the至于如何在此迁移期间修复代码,您应该更改
public abstract class AbstractBaseServiceImpl<R extends PagingAndSortingRepository<E, K> ...
into进入
public abstract class AbstractBaseServiceImpl<R extends JpaRepository<E, K> ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.