rewrite-spring icon indicating copy to clipboard operation
rewrite-spring copied to clipboard

2.4-2.5: Spring Data JPA

Open fabapp2 opened this issue 4 years ago • 2 comments

Spring Boot 2.4 to 2.5 upgrade - Spring Data JPA

Spring Boot 2.5 Release Notes

Spring Data JPA introduces a new getById method which replaces getOne. If you find your application is now throwing a LazyLoadingException please rename any existing getById method to getXyzById (where xyz is an arbitrary string). For more details, please read the updated Spring Data JPA reference documentation.

Recipe for repositories with a getById() method

Condition

Any class inheriting from org.springframework.data.jpa.repository.JpaRepository and declaring a getById(ID) method is found

Migration

  • for all types inheriting from JpaRepository
    • if method declaration with name public ENTITY getById(ID) exists
      • rename method to get<ENTITY>ByName(<ID>)

Scenarios

Custom JpaRepository declares getById(ID)**

given

public class My {}
import org.springframework.data.jpa.repository.JpaRepository;

public interface MyRepository extends JpaRepository<My,Long> {
    My getById(Long id);
}

expected

public class My {}
----
import org.springframework.data.jpa.repository.JpaRepository;

public interface MyRepository extends JpaRepository<My,Long> {
    My getMyById(Long id);
}

Recipe for calls to getOne()

Condition

Any method call to org.springframework.data.jpa.repository.JpaRepository getById(ID) found.

Migration

  • Find calls to org.springframework.data.jpa.repository.JpaRepository getOne(<TypeOfPK>)
  • Rename method call to getById(<TypeOfPK>)

Scenarios

Class calls JpaRepository.getByOne()

given

@Entity
public class My {
    @Id
    @GeneratedValue
    private Long id;
}
interface MyRepository extends JpaRepository<My, Long> {
}
public class SomeClass {
    @Autowired
    private MyRepository myRepository;
    public void method() {
        myRepository.getOne(1L); 
    }
}

expected

public class SomeClass {
    @Autowired
    private MyRepository myRepository;
    public void method() {
        myRepository.getById(1L); 
    }
}

The recipes must be executed in the given order.!

fabapp2 avatar Dec 15 '21 16:12 fabapp2

Hi @fabapp2

for all types inheriting from JpaRepository if method declaration with name public ENTITY getById(ID) exists rename method to get<ENTITY>ByName(<ID>)

suggestion:

for all types inheriting from JpaRepository
        if method declaration with name public ENTITY getById(ID) exists 
        and get<ENTITY>ById(<ID>) does not exist
        then rename method to get<ENTITY>ById(<ID>)
        and then schedule a ChangeName getById -> get<ENTITY>ById recipe via Recipe#doNext

finally schedule a ChangeName getOne -> getBy id recipe via Recipe#doNext

pway99 avatar Jan 14 '22 19:01 pway99

It looks like this was partially implemented in

  • https://github.com/openrewrite/rewrite-spring/issues/99
  • https://github.com/openrewrite/rewrite-spring/issues/98

https://github.com/openrewrite/rewrite-spring/blob/b13ef1ddf0767573346f986457b9c196721ee524/src/main/resources/META-INF/rewrite/spring-data-25.yml#L29-L36

Those do not seem to take conflicts into account, but I expect those to be rare, and likely to fail safely with a compiler error.

timtebeek avatar Nov 29 '23 10:11 timtebeek

Closing this one as mostly done.

timtebeek avatar Jun 28 '24 08:06 timtebeek