Spring Data optional parameter in query method

后端 未结 4 1024
有刺的猬
有刺的猬 2020-12-02 22:21

I want to write some query methods in repository layer. This method must ignore null parameters. For example:

List findByBarAndGoo(Bar barParam,          


        
4条回答
  •  有刺的猬
    2020-12-02 22:48

    Too late to answer. Not sure about relationship between Bar and Goo. Check if Example can helps you.

    It worked for me. I have a similar situation, entity User have set of attributes and there is findAll method which search user based on attributes(which are optional).

    Example,

      Class User{
        String firstName;
        String lastName;
        String id;
      }
    
      Class UserService{
         // All are optional
         List findBy(String firstName, String lastName, String id){
            User u = new User();
            u.setFirstName(firstName);
            u.setLastName(lastName);
            u.setId(id);
    
            userRepository.findAll(Example.of(user));
            // userRepository is a JpaRepository class
         }
      }
    

提交回复
热议问题