简体   繁体   English

spring 数据 3.0 PagingAndSortingRepository,从 2.0 迁移

[英]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.

相关问题 TestRestTemplate-从Spring Boot 1.5迁移到2.0 - TestRestTemplate - migrating from spring boot 1.5 to 2.0 Spring Data Rest PagingAndSortingRepository AbstractMethodError(RepositoryFactorySupport) - Spring Data Rest PagingAndSortingRepository AbstractMethodError (RepositoryFactorySupport) 在Spring Data JPA的PagingAndSortingRepository中查找项目页面 - Find item page in PagingAndSortingRepository in Spring Data JPA 在将我的应用程序从(spring-webmvc-3.0.5和tile 2.0)迁移到(spring-webmvc-3.2.8 tile 3.0)时获得Bean创建异常 - Getting bean creation exception while migrating my application from (spring-webmvc-3.0.5 and tiles 2.0) to (spring-webmvc-3.2.8 tiles 3.0) Spring Data JPA PagingAndSortingRepository 对加密数据而不是解密数据进行排序 - Spring Data JPA PagingAndSortingRepository sorts encrypted data instead of decrypted data 从Spring Boot 1.5迁移到2.0时获得BeanInstantiationException - while migrating from spring boot 1.5 to 2.0 getting BeanInstantiationException 与 spring 数据 jdbc 一起使用时,PagingAndSortingRepository 方法会抛出错误 - PagingAndSortingRepository methods throw error when used with spring data jdbc Neo4j 2.0与Spring Data 3.0 Rest一起启动时的异常 - Exception on Startup with Neo4j 2.0 with Spring Data 3.0 Rest 如何使用Spring Data Rest和PagingAndSortingRepository处理异常? - How can I handle exceptions with Spring Data Rest and the PagingAndSortingRepository? 使用spring-data-jpa中的PagingAndSortingRepository获取所有记录 - fetch all records using PagingAndSortingRepository in spring-data-jpa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM