user2632905
user2632905

Reputation: 267

What is the Spring Data JPA method to use findBy for multiple fields and also use Containing clause for all the fields

I have a class called Profile and its JPA repository ProfileRepo I'm trying to use findBy method to find names using first name or middle name or last name and also using containing clause.

public class Profile{
    private String firstName;
    private String middleName;
    private String lastName;

    //getters and setters
}

Am using the following query in the JPA repository but it is not accepting the method

List<Profile> findByLastNameContainingOrFirstNameContainingOrMiddleNameContainingAllIgnoreCase(String firstName,
        String lastName,String midName);

Kindly help out.

Upvotes: 13

Views: 51531

Answers (1)

Cepr0
Cepr0

Reputation: 30309

Try this:

List<Profile> findByFirstNameIgnoreCaseContainingOrLastNameIgnoreCaseContainingOrMidNameIgnoreCaseContaining(String firstName, String lastName, String midName);

or this:

@Query("select p from Profile p where upper(p.firstName) like concat('%', upper(?1), '%') or upper(p.lastName) like concat('%', upper(?2), '%') or upper(p.midName) like concat('%', upper(?3), '%')")
List<Profile> getByNames(String firstName, String lastName, String midName);

Upvotes: 28

Related Questions