54

How should I build my findBy method name so I can implement a where clause -

statusCode != 'Denied'

Is this be an option?

findByStatusCodeNotIn(List<String> statusCode);

What if I want to just pass a String instead of a list?

Thank You

Jay Kumo
  • 671
  • 1
  • 5
  • 9

4 Answers4

73

Have you taken a look at the documentation about this in the Spring Data JPA docs?

#findByStatusCodeNot(String statusCode);

It's akin the example in the docs like:

#findByLastnameNot
LuscaDev
  • 329
  • 3
  • 8
Dovmo
  • 8,121
  • 3
  • 30
  • 44
  • Can u help me with: @Query(value = "select * from test where path <@ 'a.b.c' and path <> 'a.b.c'", nativeQuery = true) List getAllPath(@Param("pathToSearch") String pathToSearch); – Ramanuj Jun 23 '20 at 01:08
  • i guess @Ramanuj you should open a new question for that. Also Spring JPA CRUD repos cant solve every scenario – tgkprog Sep 05 '21 at 15:39
23

According to the Spring Data JPA Documentation on https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

you can achieve this by using the "Not" keyword. forinstance your method would be

 findByStatusCodeNot(String statusCode);

This means to achieve "statusCode != 'Denied'", you would call the method as

 findByStatusCodeNot('Denied');
Winter MC
  • 355
  • 2
  • 7
5

@Dovmo is right, but also keep in mind that if you are operating on String data, you may have to take case into account, i.e. findByStatusCodeNot(String statusCode) will find only records that are not 'Denied' as is, but not for instance 'DeniEd'. For case-insensitive searches, you can append IgnoreCase - findByStatusCodeNotIgnoreCase(String statusCode)

In general, please follow the link that @Dovmo gave.

aberrant80
  • 12,815
  • 8
  • 45
  • 68
Yuriy Tsarkov
  • 2,461
  • 2
  • 14
  • 28
0

According to the spring JPA repository docs we can use the <> symbol in place of != so the query would be statusCode <> 'Denied' this would be helpful if you want to write a @Query instead of the inbuild repository methods.