Reputation: 714
I'm using Spring Boot 2.2.5.RELEASE with Java 8 and PostgreSQL and I need to retrieve some data from a table. To do that I can receive some optional values and they need to be case sensitive (mostly because some of them are Hex Strings). To handle that I'm using a CRUD repository with a query like the example bellow:
@Repository
public interface MyRepository extends CrudRepository<MyStuff, String> {
@Query("SELECT s FROM Stuff s WHERE (:name is null or upper(s.name) = upper(:name)) "
+ "and (:age is null or upper(s.age) = upper(:age)) "
+ "and (:status is null or upper(s.status) = upper(:status))")
List<MyStuff> findStuff(@Param("name") String name,
@Param("age") String age,
@Param("status") String status);
I've removed some information to make it clear, but the gist of it is there. The problem is, only one field must have a value, the others can be null. It should be simple enough, because I'm checking for null before casting the fields to upper case, but when I try to run it I get an error because the null values are being interpreted as bytea
and so there is no method to change its case.
How can I handle it without having a method in java to check for nulls and change to upper case on Java side?
Upvotes: 2
Views: 1172
Reputation: 16452
I wouldn't recommend that you do that. A DBMS will use a pretty conservative query plan when encountering something like that. You should use Spring-Data Specifications for conditional filters like this.
Upvotes: 1