Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Query with Lower and Wildcards

This Question refers to Spring Data JPA.

I need a (native) @Query with the Lower Function and Wildcards. But I dont get it to work.

Here is a oversimplified version of what I am looking for:

@Query(value = "SELECT * FROM CAR c WHERE LOWER(c.model) LIKE LOWER(%:model%)", nativeQuery = true)
List<Car> findByModelMatching( @Param("model") String model );

LOWER(%:model%) -> not working! LOWER(:model) -> works!

I am aware of that such a query can be easily re-written as:

List<Car> findByModelContainingIgnoreCase( String model );

But I am not looking for a Named Query.

My Question is: How to combine LOWER (or even UPPER) with WildCards in a @Query!

Cheers

like image 661
Dachstein Avatar asked Mar 28 '17 07:03

Dachstein


Video Answer


1 Answers

The LOWER function accepts strings, you should write LOWER('%' || :model || '%') if you are on Oracle for example.

like image 70
Arnold Galovics Avatar answered Oct 07 '22 11:10

Arnold Galovics