0

Hi I am new to spring boot. I have a table having the following attributes: id,firstName,secondName,lastName.

Now I need to write a query (in repository), to find all those rows in my table whose firstName or secondName or lastName matches with the string I am passing.

eg: if I am passing 'foo' then it should search all the three columns and return those rows having 'foo' in any of them(this is pattern matching).

How can I do that? Thanks in advance.

Eklavya
  • 17,618
  • 4
  • 28
  • 57
VANDANA S
  • 31
  • 1
  • 4
  • You can try like this findByFirstnameLikeOrLastNameLike(String name, String name) in repository ? – Eklavya Apr 10 '20 at 05:30
  • Does this answer your question? [Spring Data - Multi-column searches](https://stackoverflow.com/questions/25872637/spring-data-multi-column-searches) – Hemant Apr 10 '20 at 05:40
  • @AbinashGhosh i tried it.. and i got this error: Failed to create query for method public abstract java.util.List com.example.project.core.repository.PersonRepository.findByFirstNameLikeOrLastNameLike(java.lang.String)! null: NoSuchElementException – VANDANA S Apr 10 '20 at 05:43
  • I just give you a example not exact query and try my answer bellow by changing as you want – Eklavya Apr 10 '20 at 05:46

2 Answers2

1

You can use Like query on multiple column like this way

public interface UserRepository extends PagingAndSortingRepository<User,Long> {

    @Query(value="select u from User u where u.firstName = %searchtext% or u.lastName= %searchtext% or u.secondName= %searchtext%")    
    Page<User> findByAllColumns(@Param("searchtext") String searchtext, Pageable pageable);
}
Eklavya
  • 17,618
  • 4
  • 28
  • 57
0

you can try with the below code

public List<Employee> findByFirstNameIgnoreCaseContainingOrSecondNameIgnoreCaseContainingOrLastNameIgnoreCaseContaining(String firstName,String secondName,String lastName);
dassum
  • 4,727
  • 2
  • 25
  • 38