Spring Data - ignore parameter if it has a null value

前端 未结 9 1545
暖寄归人
暖寄归人 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:34

    You could do that too.

    Repository:

    `MyObject findByParameterOneAndParameterTwo( String parameterOne, String parameterTwo);`
    

    if you pass a null parameterTwo, the generated JPQL will include the IS NULL condition:

    `myobject0_.parameterTwo is null`
    

    Ex: repository.findByParameterOneAndParameterTwo("D", null);

    Reference: https://www.baeldung.com/spring-data-jpa-null-parameters#query-methods

提交回复
热议问题