简体   繁体   中英

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.

Previously when PagingAndSortingRepository extended CrudRepository in spring data 2.0, we had this piece of code which worked just fine

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

E entity = repository.findById(id).orElse(null);

Because PagingAndSortingRepository doesn't have findById anymore, since it's inherited from 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. 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 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

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> ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM