openrewrite / rewrite-spring

OpenRewrite recipes for Spring projects.
Apache License 2.0
265 stars 78 forks source link

2.4-2.5: Spring Data JPA #123

Closed fabapp2 closed 5 months ago

fabapp2 commented 2 years ago

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

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

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

pway99 commented 2 years ago

Hi @fabapp2

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

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
timtebeek commented 1 year ago

It looks like this was partially implemented in

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 commented 5 months ago

Closing this one as mostly done.