From c0e56f44aa4af1bfd81deafd0d3206cf19a60eae Mon Sep 17 00:00:00 2001 From: mayuresh1404 <145109347+mayuresh1404@users.noreply.github.com> Date: Sun, 23 Mar 2025 21:17:54 +0530 Subject: [PATCH] Update ProductRepository.java --- .../example/repository/ProductRepository.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java b/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java index 15ddb8a..248134a 100644 --- a/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java +++ b/src/main/java/com/javatechie/crud/example/repository/ProductRepository.java @@ -2,8 +2,23 @@ import com.javatechie.crud.example.entity.Product; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface ProductRepository extends JpaRepository { -public interface ProductRepository extends JpaRepository { Product findByName(String name); + + // ✅ Advanced Search Query + @Query("SELECT p FROM Product p WHERE " + + "(:name IS NULL OR LOWER(p.name) LIKE LOWER(CONCAT('%', :name, '%'))) " + + "AND (:minPrice IS NULL OR p.price >= :minPrice) " + + "AND (:maxPrice IS NULL OR p.price <= :maxPrice)") + List advancedSearch(@Param("name") String name, + @Param("minPrice") Double minPrice, + @Param("maxPrice") Double maxPrice); } +