CR Sardar
CR Sardar

Reputation: 1026

JPA Multiple Like and AND clasues

I am new to JPA, how Can I convert bellow SQL statement to JPA statement?

SELECT * FROM Customers WHERE (CustomerName LIKE '%Chitta%' OR Address LIKE '%Permanent%') AND Country = "UK" AND City = "London";

My Customers table has following attributes -

CustomerID, CustomerName ContactName, Address, City, PostalCode, Country

I am using

"org.springframework.boot:spring-boot-starter-data-jpa"

Upvotes: 0

Views: 302

Answers (1)

Puneet Kundliya
Puneet Kundliya

Reputation: 344

You can use the following below query to work with. Also native query true help you run the native SQL query. If you are providing the parameter through the method argument then use this query.

@Query(value = "SELECT * FROM Customers WHERE (CustomerName LIKE %:cname% OR Address LIKE %:add%) AND Country = :country AND City = :city",nativeQuery = true)
List<Customers> getListCustomers(@Param("cname")String CustomerName,@Param("add") String Address, @Param("country") String, @Param("city")String City)

If you are hardcoding the fields then you can directly use your query in JPA.

For more understanding regarding the JPA query go through this link

Upvotes: 1

Related Questions