1

I have a search with JPA query using Spring Data like this :

My interface repository :

public interface AnnonceDao extends   JpaRepository<Annonce, Integer> {
    Page<Annonce> findByTitreContaining(String titre,Pageable page);
}

when I look for something like Hello Friend I get all entities that have Hello Friend concatened , I want something to get Friend Hello too or any entity that contains Hello or Friend. what I can use as Query, thanks for your help.

Omar B.
  • 441
  • 11
  • 39

1 Answers1

0

This should work:

public interface AnnonceDao extends   JpaRepository<Annonce, Integer> {
    Page<Annonce> findByTitreContainingAndTitreContaining(
        String part1, 
        String part2,
        Pageable page
    );
}

If the number of terms you want to pass is unknown at compile time, as you explain in the comment you can use Specifications.

This leaves the task of creating the specification to the client of the repository. If you don't like that you can provide a custom method and dynamically create a query inside. Again you have the choice of multiple technologies:

  • SQL
  • JQL
  • QueryDsl

QueryDsl looks actually really nice and readable as you can see in the linke example.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • thanks Jens for your answer , actually the number of words is known , it s kind of search so I don t have number of words in my string. – Omar B. May 31 '17 at 17:41