Spring Data JPA findOne() change to Optional how to use this?

前端 未结 5 1184
一生所求
一生所求 2020-12-12 10:26

I\'m learning SpringBoot2.0 with Java8.

And I followed some blog-making tutorial example.

The tutorial source code is:



        
5条回答
  •  天命终不由人
    2020-12-12 11:25

    I always write a default method "findByIdOrError" in widely used CrudRepository repos/interfaces.

    @Repository 
    public interface RequestRepository extends CrudRepository {
    
        default Request findByIdOrError(Integer id) {
            return findById(id).orElseThrow(EntityNotFoundException::new);
        } 
    }
    

提交回复
热议问题