46

My understanding is, that with Spring data JPA I cannot have a query method to fetch all rows where a column equals a given non-null method parameter and use the same method to fetch all rows where this column is NULL when the method parameter is null.

Is that correct?

So I have to distinguish this in my JAVA code and I must use a separate query method explicitly asking for null values, like in the example below?

// Query methods
List<Something> findByParameter(Parameter parameter);
List<Something> findByParameterIsNull();

...

List<Something> result = new ArrayList<>();

if (parameter == null)
  result = findByParameterIsNull();
else
  result = findByParameter(parameter);

That's bad, if I have 4 parameters which could be null and would have to code 16 different query methods.

Sebastian S.
  • 1,545
  • 6
  • 24
  • 41

8 Answers8

26

You are right.

A request has been made to support better handling of null parameters. https://jira.spring.io/browse/DATAJPA-121

In your case, i would advise you to write your repository implementation and to use a custom CriteriaQuery to handle your case.

Also you can use the @Query annotation with the is null syntax :

@Query("[...] where :parameter is null"
public List<Something> getSomethingWithNullParameter();

EDIT

Since Spring data jpa 2.0, spring now supports @Nullable annotation. This can be helpful to handle null parameters passed.

From the documentation :

@Nullable – to be used on a parameter or return value that can be null.

Laurent B
  • 2,200
  • 19
  • 29
  • 2
    I have extend the answer: When using the Spring data JPA `@Query` annotation, you have to use two separate query methods. One for handling null values and one for non-null values. But when using a query method which is parsed by the Spring data parser (without `@Query` annotation), it is possible to handle null or non-null values with one method! – Sebastian S. May 18 '15 at 13:59
  • 2
    The @Query `:parameter is null` doesn't work. It fails with an exception, but even if it wouldn't, SQL treats nulls as unknown. Thus unknown is not equal with unknown ... Basically `null` is not equal with `null` in SQL. – Stef Jan 22 '16 at 07:40
  • 7
    How Nullable help in this question? I don't think Spring Data will turn the query into "is null" if we annotate the parameter as @Nullable. – Sam YC Jun 05 '18 at 05:14
17

i found something...if u put the parameter in the jpa method like this

@Param("value") String value,

then it can be null and in the query you will have this condition:

(table.value = :value OR :value IS NULL)

if the value is null it will automatically return true and if is not null, it will search that value in the table.

fvukovic
  • 699
  • 1
  • 7
  • 14
  • 3
    One thing that got me was it wasn't working if I did (:value IS NULL OR table.value = :value) It was telling me I needed to cast :value But doing it in the order you wrote works. – dfritch Jan 22 '19 at 03:01
  • 2
    :vales is null won't work if the value is a collection. – Anuj Pandey May 30 '19 at 12:34
  • I found this solution better (and simpler) rather than implementing JpaSpecificationExecutor or QueryDslPredicateExecutor; [reference](https://stackoverflow.com/questions/20280708/filtering-database-rows-with-spring-data-jpa-and-spring-mvc). Also I followed this [blog](https://www.baeldung.com/spring-data-jpa-null-parameters) – Paco Mendez Jul 15 '20 at 18:57
  • 2
    It will only skip the field for select query for null values passd. may lead to extra result set. – Vivek Singh Feb 04 '21 at 07:30
15

It seems Query by Example might be what you need.

Query by Example is a new feature in Spring Data (since version Hopper, out April 2016), which allows one to create simple dynamic queries with a code like this

Person person = new Person();                          
person.setFirstname("Dave");                           

ExampleMatcher matcher = ExampleMatcher.matching()     
  .withIncludeNullValues();                        

Example<Person> example = Example.of(person, matcher);

personRepository.count(example);
personRepository.findOne(example);
personRepository.findAll(example);

Methods count/findOne/findAll that take an instance of org.springframework.data.domain.Example as a parameter (and some of them also take sorting/pagination parameters) are coming from org.springframework.data.repository.query.QueryByExampleExecutor<T> interface, which is extended by org.springframework.data.jpa.repository.JpaRepository<T, ID extends Serializable> interface.

In short, all JpaRepository instances now have these methods.

pillingworth
  • 3,238
  • 2
  • 24
  • 49
mvmn
  • 3,717
  • 27
  • 30
7

Today as of Jun 2018, by looking at https://jira.spring.io/browse/DATAJPA-121, the query will automatically form is null if your parameter is null.

I did that in my project, it is true:

compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '2.0.7.RELEASE'

--

public interface AccountDao extends CrudRepository<T, ID> {

    //this can accept null and it will become isNull
    public List<MyAccount> findByEmail(String email);

}

if parameter is null:

select
        myaccount0_.id as id1_0_,
        myaccount0_.email as email2_0_,
        myaccount0_.password as password3_0_,
        myaccount0_.user_id as user_id4_0_ 
    from
        my_account myaccount0_ 
    where
        myaccount0_.email is null

if parameter is not null:

select
        myaccount0_.id as id1_0_,
        myaccount0_.email as email2_0_,
        myaccount0_.password as password3_0_,
        myaccount0_.user_id as user_id4_0_ 
    from
        my_account myaccount0_ 
    where
        myaccount0_.email=?
11:02:41.623 [qtp1507181879-72] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [testing@hotmail.com] 

Then it comes to an interesting question, some developers want better control to ignore the parameter in query if it is null, this is still being under investigating in https://jira.spring.io/browse/DATAJPA-209.

Sam YC
  • 10,725
  • 19
  • 102
  • 158
  • I'm using spring-data-jpa 2.4.2 and @Query annotated method with a param like "@Param("messageId") String messageId" requires it is not null else it throws an Exception – Gamby Apr 04 '23 at 10:03
4

In my case membershipNumber is nullable, and I have handled it this way. This will handle all the cases where table.membershipNumber is null too.

      @Query(value = "SELECT pr FROM ABCTable pr " +
            "WHERE LOWER(pr.xyz) = LOWER(:xyz) " +
            "and LOWER(pr.subscriptionReference) = LOWER(:subscriptionReference) " +
            "and pr.billId = :billId " +
            "and ((pr.membershipNumber = :membershipId) or (pr.membershipNumber = null and :membershipId = null))")
    List<PaymentRequest> getSomething (@Param("xyz") String xyz,
                                                 @Param("subscriptionReference") String subscriptionReference,
                                                 @Param("billId") Integer billId,
                                                 @Param("membershipId") String membershipNumber);

3

While this has been answered and the accepted answer is relevant to the current question but there is another way to handle your null parameters in a JpaRespository. Posting this here as this can be leveraged when someone wants to query by ignoring fields when null and have dynamic query built. The below code sample should demonstrate the same

public class User{
  private String firstName;
  private String lastName;
}

import javax.persistence.criteria.Predicate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User,Long>{

  public Page<AppUser> findAll(Specification<AppUser> user,Pageable page);

  public default Page<AppUser> findAll(User user,Pageable page){
    return findAll(search(user),page);
  }

  static Specification<User> search(User entity) {
    return (root, cq, cb) -> {
      //To ensure we start with a predicate
      Predicate predicate = cb.isTrue(cb.literal(true));
      if(entity.getFirstName() != null && !entity.getFirstName().isBlank()) {
        Predicate _predicate = cb.like(cb.lower(root.get("firstName")), "%"+entity.getFirstName().toLowerCase()+"%");
        predicate = cb.and(predicate,_predicate);
      }
      if(entity.getLastName() != null && !entity.getLastName().isBlank()) {
        Predicate _predicate = cb.like(cb.lower(root.get("lastName")), "%"+entity.getLastName().toLowerCase()+"%");
        predicate = cb.and(predicate,_predicate);
      }
      return predicate;
    }
  }
}
ganaraj
  • 420
  • 3
  • 13
  • This is the example I've been looking for, however i'm receiving an error on build: `No property findAll found for type User.` – Pezetter Aug 20 '21 at 18:13
  • 1
    A full stack might help, or it could be due to JPA2 enabling annotations not added. In this case the findAll methods is explicitly defined. – ganaraj Aug 24 '21 at 04:25
2

I was able to apply IS NULL appropriately in case of null input using below workaround.

    @Query("SELECT c FROM ConfigRLLOAContent c WHERE ((:suffixId IS NOT NULL AND c.suffixId = :suffixId) OR (:suffixId IS NULL AND c.suffixId IS NULL))")
Optional<ConfigRLLOAContent> findByRatableUnitId(@Param("suffixId") String suffixId);

Above approach will apply filters only when suffixId is non-null, else, IS NULL filter will be applied.

There's also an issue raised on github, to which introduction of @NullMeans is proposed here.

DonAjit
  • 46
  • 3
0

I had the same issue with similar task - one parameter in the query was optional, so to get rid of this error, I managed to use the following query with 2 casts:

@Query(value = "select distinct name from table "
        + "where coalesce(cast(table.field_optional as text) = cast(?1 as text), true) "
        + "and lower(table.non_optional_field) like ?2 "
        + "limit ?3", nativeQuery = true)
List<String> method(String optionalParam, String param, int limit);

This coalesce part would transform into simple 'true' if optionalParam is null