Should I use Java 8 default methods for manually implemented Spring Data repository methods?

前端 未结 3 1393
感情败类
感情败类 2021-02-12 19:00

When using the new Spring Data Evans release it\'s nice to be able to use some of the nice stuff that came with java 8. One of them is default implementations in interfaces. The

3条回答
  •  太阳男子
    2021-02-12 19:51

    You don't get the EntityManager in an interface, although you might be able to work around it by doing a lookup.

    But why are you even doing this? Spring Data JPA already supports the Optional return type so you don't need to implement it. Spring Data will do it for you.

    public interface UserRepository extends JpaRepository {
    
        Optional findByLoginIgnoreCase(String login) {
    }
    

    The code above should be all you need. You could even specify a query with @Query if you would need it.

    A Sample can be found here.

提交回复
热议问题