2.4-2.5: Spring Data JPA
Spring Boot 2.4 to 2.5 upgrade - Spring Data JPA
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>)
- rename method to
- if method declaration with name
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.!
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
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.
Closing this one as mostly done.