Spring Boot 2.5.5

In 2.4.4 I was able to use either findById or getById, but in 2.5.5 only findById returns an object that I can safely use. The object returned by getById will throw LazyInitializationException with the message "No Session" if I try to access any attributes, e.g. name in this example:

@Repository
public interface FooRepository extends CrudRepository<Foo, Long> {

    Optional<Foo> findById(Long aLong);

    Foo getById(long id);

}

@Data
@Entity
public class Foo {

    @Id
    private long id;

    private String name;

}

Sample service accessing the repo

@Service
@RequiredArgsConstructor
@Slf4j
public class FailService {

    private final FooRepository fooRepo;

    @PostConstruct
    public void init() {
        Foo f = fooRepo.findById(1L).get();
        log.info("findById foo.id: " + f.getId());
        log.info("findById foo.name: " + f.getName());

        f = fooRepo.getById(1);
        log.info("getById foo.id: " + f.getId());
        log.info("getById foo.name: " + f.getName());
    }
}

The first three lines print, the final one throws

org.hibernate.LazyInitializationException: could not initialize proxy [com.example.demo.Foo#1] - no Session

Minimal case attached with a junit test that demonstrates the problem.

getById-regression.zip

Comment From: wilkinsona

Thanks for the sample.

This appears to be a change in behaviour in Spring Data JPA. With Spring Boot 2.5.5 and Spring Data downgraded to 2020.0 (the version used in Spring Boot 2.4.x) the problem does not occur. There's some similarity with https://github.com/spring-projects/spring-data-jpa/issues/2261, but there's no naming collision here as you're using CrudRepository rather than JpaRepository.

Can you please raise a Spring Data JPA issue so that the Data team can investigate?