Spring Data - ignore parameter if it has a null value

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

    Try this Kolobok

    @FindWithOptionalParams
    Iterable<MyObject> findByParameterOneAndParameterTwo( String parameterOne, String parameterTwo);
    
    0 讨论(0)
  • 2020-11-29 04:21

    I'm not sure it is possible with repository methods naming but you can use @Query like

    (:parameterOne is null or parameter1 = :parameterOne) and (:parameterTwo is null or parameter2 = :parameterTwo)
    
    0 讨论(0)
  • 2020-11-29 04:22

    I am not sure if its possible using Repo as a separate class but you can use StringBuilder append query with option parameter. This will definitely work

     StringBuilder queryBuilder = new StringBuilder();
        queryBuilder.append("select p.name from personDemographic p "); 
        Boolean flag = true;
        if(parameterOne != null){
          if(flag){
              queryBuilder.append("where condition...");
                flag=false;
            } 
          }
        if(parameterOne != null){
        if(flag){
         queryBuilder.append("where condition...");
         flag = false;
        }else{
          queryBuilder.append("and condition...");
        }
       Query query = entityManager.createQuery(queryBuilder.toString());
    
    0 讨论(0)
  • 2020-11-29 04:24

    One solution that's missing here is Spring Data JPA's Query By Example feature and leverage the ExampleMatcher#ignoreNullValues, which is built exactly to solve this problem. A custom query and query builder are not necessary.

    This Spring Data query:

    ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues();
    Example<MyObject> exampleQuery = Example.of(new MyObject("foo", null), matcher);
    List<MyObject> results = repository.findAll(exampleQuery);
    

    Yields a query that looks like:

    select * 
    from myObject 
    where parameter1 = "foo"
    

    While the following:

    ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues();
    Example<MyObject> exampleQuery = Example.of(new MyObject("foo", "bar"), matcher);
    List<MyObject> results = repository.findAll(exampleQuery);
    

    Yields:

    select * 
    from myObject 
    where parameter1 = "foo"
    and parameter2 = "bar"
    

    Very cool!

    Note: One thing you'll have to do to your Repository interface is add the QueryByExample interface. You can do this either by extending the QueryByExample interface directly, or implicity via the JpaRepository:

    public interface MyObjectRepository extends JpaRepository<MyObject, Long> {}
    
    0 讨论(0)
  • 2020-11-29 04:31

    Try this one,

          @Query(value = "SELECT pr FROM ABCTable pr " +
            "WHERE((pr.parameterOne = :parameterOne) or (pr.parameterOne = null and :parameterOne = null)) and 
            ((pr.parameterTwo = :parameterTwo) or (pr.parameterTwo = null and :parameterTwo = null)) ")
          List<PaymentRequest> getSomething (@Param("parameterOne") String parameterOne,
                                                 @Param("parameterTwo") String parameterTwo);
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题