Spring data repository's method to find by the field of a field

后端 未结 1 1962
深忆病人
深忆病人 2020-12-29 07:47

I\'ve two entities, a user and a registered user.

A registered user has a field of type user. I would like to have a method in the spring data repository related to

相关标签:
1条回答
  • 2020-12-29 08:40

    Spring Data (at least 1.12.x version) uses PropertyPath#from method to extract path to a property for a predicate constructed from method name. According to sources it uses underscore as "field separator". So first variant is as follows

    public interface RegisteredUserRepository extends CrudRepository<RegisteredUser,String> {
        List<RegisteredUser> findRegisteredUserByUser_Username(String username);
    }
    

    There is also code which treat an uppercase char as field separator if whole field name is not found. So if you don't have a userUsername field in RegisteredUser second varian is

    public interface RegisteredUserRepository extends CrudRepository<RegisteredUser,String> {
        List<RegisteredUser> findRegisteredUserByUserUsername(String username);
    }
    
    0 讨论(0)
提交回复
热议问题