Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data jpa findBy...... with multi column but the save search text

i'm using spring data jpa, and have a query to search text from the whole columns.

for example:

repository.findByNameContainingOrAliasContaining(name, alias, pageable)

And the name and alias are the same value, and i have to write like

string name = text; string alias = text;
repository.findByNameContainingOrAliasContaining(name, alias, pageable)

and actually, i have 5 columns to be matched, so how can i stop writing the same stupid code ? and make the code like: repository.findByNameContainingOrAliasContaining(text, pageable) (this writing now will cause ".NoSuchElementException")

repository

like image 421
Awakening Avatar asked Oct 02 '22 08:10

Awakening


1 Answers

When you face a limitation of implicit queries, you can always switch to explicit @Query:

@Query("select f from Foo f where f.name like %?1% or f.alias like %?1% or ...")
public List<Foo> findByAnyColumnContaining(String text, Pageable pageable);
like image 82
axtavt Avatar answered Oct 05 '22 12:10

axtavt