我正在执行@Getmap请求,以使用多个参数搜索产品.
我的ProductController.Java
@GetMapping("/search")
ResponseEntity<ResponseObject> findBySearch(@RequestParam(required = false) Integer productId,
@RequestParam(required = false) String productName,
@RequestParam(required = false) Integer brandId,
@RequestParam(required = false) Integer categoryId,
@RequestParam(required = false) Integer modelYear) {
List<Product> foundProducts = repository.findProductBySearch(productId, productName, brandId, categoryId, modelYear);
return (!foundProducts.isEmpty()) ?
ResponseEntity.status(HttpStatus.OK).body(new ResponseObject(HttpStatus.OK.toString(), "Query successfully", foundProducts))
:
ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResponseObject(HttpStatus.NOT_FOUND.toString(), "Cannot find product with provided model", ""));
}
我的ProductRepository.Java
@Query("SELECT p FROM Products p WHERE (:productId is null or p.productId = :productId) and " +
"(:p.productName is null or p.productName = :productName) and " +
"(:p.brandId is null or p.brandId = :brandId) and " +
"(:p.categoryId is null or p.categoryId = :categoryId) and " +
"(:p.modelYear is null or p.modelYear = :modelYear)")
List<Product> findProductBySearch(@Param("productId") Integer productId,
@Param("productName") String productName,
@Param("brandId") Integer brandId,
@Param("categoryId") Integer categoryId,
@Param("modelYear") Integer modelYear);
My Product.java
@Entity
@Table(name = "Products")
public class Product {
@Id
@SequenceGenerator(
name = "product_sequence",
sequenceName = "product_sequence",
allocationSize = 1
)
@GeneratedValue (
strategy = GenerationType.SEQUENCE,
generator = "product_sequence"
)
@Column(name = "product_id")
int productId;
@Column(name = "product_name")
String productName;
@Column(name = "brand_id")
int brandId;
@Column(name = "category_id")
int categoryId;
@Column(name = "model_year")
int modelYear;
@Column(name = "list_price")
double listPrice;
//getters, setters
当我运行项目时,它产生了这个错误
org.hibernate.query.SemanticException: A query exception occurred [SELECT p FROM Products p WHERE (:productId is null or p.productId = :productId) and (:p.productName is null or p.productName = :productName) and (:p.brandId is null or p.brandId = :brandId) and (:p.categoryId is null or p.categoryId = :categoryId) and (:p.modelYear is null or p.modelYear = :modelYear)]个
请帮我找出哪里弄错了.