Skip to content

Spring 2.7: Recipe to replace getById with getReferenceById #515

Closed
@lucashan

Description

@lucashan

What problem are you trying to solve?

As of Springboot v2.7, both getOne() and getById() methods have been deprecated for JpaRepository. The documentation states that these deprecated methods should be replaced with getReferenceById(ID).

We should include this recipe for Spring 2.7, for example:

type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.spring.data.UseJpaRepositoryGetReferenceById
displayName: Use `JpaRepository#getReferenceById(ID id)`
description: '`JpaRepository#getById(ID)` was deprecated in 2.7.'
recipeList:
  - org.openrewrite.java.ChangeMethodName:
      methodPattern: org.springframework.data.jpa.repository.JpaRepository getById(..)
      newMethodName: getReferenceById

Describe the situation before applying the recipe

import org.springframework.data.jpa.repository.JpaRepository;

public interface TestRepository extends JpaRepository<Test, Long> {
}
    public Test getTestById(final long id) {
        return testRepository.getOne(id);
    }

Describe the situation after applying the recipe

import org.springframework.data.jpa.repository.JpaRepository;

public interface TestRepository extends JpaRepository<Test, Long> {
}
    public Test getTestById(final long id) {
        return testRepository.getReferenceById(id);
    }

Have you considered any alternatives or workarounds?

N/A

Are you interested in contributing this recipe to OpenRewrite?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions