4

When querying by Example with JPA, is there a way to specify null matching for one column instead of all? It seems like the Null matching can only be applied to all columns.

class Entity {
  private Integer id;
  private String name;
  private Instant createdAt;
  private Instant deletedAt;
  // getters and setters
}

When querying by example, I want to include when deletedAt is actually null in the example, but ignore the null matching on other fields. In the database, the created_at column is a TIMESTAMP WITHOUT TIME ZONE column (postgresql)

Edit: Solutions proposed in Spring data query where column is null aren't applicable since they don't take advantage of querying by example

aarbor
  • 1,398
  • 1
  • 9
  • 24
  • Does this answer your question? [Spring data query where column is null](https://stackoverflow.com/questions/29448282/spring-data-query-where-column-is-null) – FishingIsLife Dec 20 '19 at 19:59
  • Anthersolution would be to use native query if that is an option. – FishingIsLife Dec 20 '19 at 20:01
  • @medTech native query wouldn't give me the benefit of querying by example. And your first link would work but also doesn't work with example it seems. I'll probably have to use Specification – aarbor Dec 23 '19 at 17:06

1 Answers1

1

You can try achieve what you want with ExampleMatcher. Specifically ExampleMatcher.includeNullValues() and / or ExampleMatcher.withIgnorePaths("deletedAt")

Try one or both:

ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll()
                                               .withIgnorePaths("deletedAt")
                                               .withIncludeNullValues();
Example<Entity> ex = Example.of(obj, exampleMatcher);
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119
  • 2
    With this setup I would need to manually ignore all of the values that are null. I want null matching to act how it does normally (ignoring nulls), except I want to match exactly when deletedAt is null – aarbor Dec 23 '19 at 16:59