Home > Software engineering >  spring data 3.0 PagingAndSortingRepository, migrating from 2.0
spring data 3.0 PagingAndSortingRepository, migrating from 2.0

Time:01-30

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 :)

CodePudding user response:

I think you should get this to work by declaring R like this:

<R extends CrudRepositor<E, K> & PagingAndSortingRepository<E, K>, …>

CodePudding user response:

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