Spring Data - ignore parameter if it has a null value

前端 未结 9 1506
暖寄归人
暖寄归人 2020-11-29 03:59

I want to have a spring data repository interface that takes two parameters. Is there a way to make it have the following behaviour?

MyObject findByParameter         


        
相关标签:
9条回答
  • 2020-11-29 04:36

    Currently this is not possible in Spring-data-jpa.

    There is a JIRA ticket regarding this which is still under investigation by the Spring team.

    However if you want a workaround you can checkout a simple criteria query example.

    0 讨论(0)
  • 2020-11-29 04:39

    I am new in Spring/JPA space,

    use 'Query By Example'

    i am using (in seviceImp) , all below arguments are optional/ depends on user choice

    `
      .
        if (!firstName.isEmpty() ) {
        staff.setFirstName(firstName);
        }
    
    
    
        if (!lastName.isEmpty() ) {
        staff.setLastName(lastName);
        }
    
        if (!ptAadhar.isEmpty() ) {
            patient.setPtAadhar(ptAadhar);
        }
    
        if (!Cell.isEmpty() ) {
            staff.setCell(Cell);
        }
    
    
          Example<StaffEntity> example = Example.of(staff);  
    
          List<StaffEntity> staffList =staffRepository.findAll(example);
           .
    
    0 讨论(0)
  • 2020-11-29 04:42

    Here is the way for do so:

    @Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null"
          + " or c.email = :email)")
        List<Customer> findCustomerByNameAndEmail(@Param("name") String name, @Param("email") String email);
    
    0 讨论(0)
提交回复
热议问题