我正在执行@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)]

请帮我找出哪里弄错了.

推荐答案

查询中的一个错误是:p.productName is null,请将其更改为(p.productName is null or p.productName = :productName)

:p.productName是一个命名参数,命名参数应该有一个值,因为:p.productName没有提供异常抛出.如果您使用的是HQL,则它应该是Bean中的属性名称;如果您使用的是原生查询,它应该是表中的列名.

对于其他列(如:p.brandId:p.categoryId:p.modelYear)也会发生同样的情况,因此您也应该对其他列执行此操作,正确的查询将为

@Query("SELECT p FROM Product p WHERE " +
            "(p.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)")

Java相关问答推荐

Jlink选项&-strie-ative-Commands";的作用是什么?

如何创建同一类的另一个对象,该对象位于变量中?

Spring data JPA/Hibernate根据id获取一个列值

Kotlin内联互操作:强制装箱

自定义批注的外推属性值

我可以在MacOS上使用什么Java函数来在适当的设备上以适当的音量播放适当的alert 声音?

有没有可能在时间范围内得到多种解决方案?

允许同时执行两个方法,但不能同时执行这两个方法

JNI:将代码打包成自包含的二进制文件

基于配置switch 的@Controller的条件摄取

无法播放音频:从资源加载库GStreamer-Lite失败

Java在操作多个属性和锁定锁对象时使用同步和易失性

当我在Java中有一个Synchronized块来递增int时,必须声明一个变量Volatile吗?

记录是类的语法糖吗?

将java.util.Date(01.01.0001)转换为java.time.LocalDate将返回29.12.0000

JPA无手术同品种器械可能吗?

如何使用外部函数从Java中获取C++ struct 的返回值&;内存API

一条Java记录可以保存多少个字段?

与配置类中的自动装配字段相反是什么意思?

在 MapStruct 中,如何在 @Mapping 和 @Named 方法中使用相同的方法参数?