It’s saying org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘productController’: Unsatisfied dependency expressed through field ‘service’: Error creating bean with name ‘productService’: Unsatisfied dependency expressed through field ‘repo’: Error creating bean with name ‘productRepo’ defined in com.example.ecomwebsitejava.repo.ProductRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Could not create query for public abstract java.util.List com.example.ecomwebsitejava.repo.ProductRepo.searchproduct(java.lang.String); Reason: Validation failed for query for method public abstract java.util.List com.example.ecomwebsitejava.repo.ProductRepo.searchproduct(java.lang.String)
Controller layer code – I have already given annotations autowired, Restcontroller
@GetMapping("/products/search")
public ResponseEntity<List<Product>> searchproduct(@RequestParam String keyword)
{
List<Product> products=service.searchproduct(keyword);
return new ResponseEntity<>(products,HttpStatus.OK);
}
Service layer code – I have already given service
public List<Product> searchproduct(String keyword){
return repo.searchproduct(keyword);
}
“
Repo layer code – I have already given Repository annotation
public interface ProductRepo extends JpaRepository<Product, Integer> {
@Query("SELECT p FROM Product p WHERE"+
"LOWER(p.name) LIKE LOWER(CONCAT('%' , :keyword, '%')")
List<Product> searchproduct(String keyword);
}
1
There is a missing ) in the query and a missing space between WHERE and LOWER clauses.
If you read the exception, the issue is not in the autowiring, but in query validation. It should be:
@Query("SELECT p FROM Product p WHERE "
+ " LOWER(p.name) LIKE LOWER(CONCAT('%' , :keyword, '%')) ")
Try it and please check all the stack trace.
0
You are missing a space after the WHERE and a closing clammer ‘)’. To avoid these exceptions in the future, I would recommend using text blocks instead of string concatenation.
public interface ProductRepo extends JpaRepository<Product, Integer> {
@Query("""
SELECT p FROM Product p
WHERE LOWER(p.name) LIKE LOWER(CONCAT('%' , :keyword, '%'))
""")
List<Product> searchproduct(String keyword);
}
1
I can see 2 things wrong with your query a missing space and a missing )
at the end. You can either fix the query or replace it by a finder method.
Fix query
Fix the query by adding a space after WHERE
and adding a closing )
for the LOWER
function. As you are probably on a newer Java you should also use textblocks, those are easier and less error prone. Using text blocks makes the query more readable as well.
@Query("""
SELECT p FROM Product p WHERE
LOWER(p.name) LIKE LOWER(CONCAT('%' , :keyword, '%'))
""")
Use a finder method
You can write a finder method which does the same, without having to write a query yourself. You can use the Containing
and IgnoreCase
keywords for that, which are understood by Spring Data.
List<Product> findByNameContainingIgnoreCase(String keyword);
These query methods and generation are particularly useful for simple cases like this.
1