Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql JPA存储库中带有@Query的多个SELECT语句_Sql_Postgresql_Spring Boot_Spring Data Jpa - Fatal编程技术网

Sql JPA存储库中带有@Query的多个SELECT语句

Sql JPA存储库中带有@Query的多个SELECT语句,sql,postgresql,spring-boot,spring-data-jpa,Sql,Postgresql,Spring Boot,Spring Data Jpa,我试图用子网执行select语句,在存储库中使用带有“nativeQuery=true”的@Query注释。 但它给了我以下错误: org.postgresql.util.PSQLException:错误:“选择”处或附近出现语法错误 以下是给我错误信息的查询 @Query(value="SELECT COUNT(cc.customer) AS tot FROM SELECT DISTINCT new RatingsAndReviews(c.customer,c.customerRating)

我试图用子网执行select语句,在存储库中使用带有“nativeQuery=true”的@Query注释。 但它给了我以下错误:

org.postgresql.util.PSQLException:错误:“选择”处或附近出现语法错误

以下是给我错误信息的查询

@Query(value="SELECT COUNT(cc.customer) AS tot FROM SELECT DISTINCT new RatingsAndReviews(c.customer,c.customerRating) FROM RatingsAndReviews AS c WHERE c.vehicle=?1 AS cc WHERE cc.customerRating=?2",nativeQuery = true)
    Integer getNoOfRatingsForStars(Vehicle vehicle,double starNumber);
该查询基本上是为客户从特定车辆的5颗星中选择特定星号的评级计数

当按如下方式执行时,仅子选择查询就提供了预期的结果

@Query("SELECT DISTINCT new RatingsAndReviews(c.customer,c.customerRating)  FROM RatingsAndReviews AS c WHERE c.vehicle=?1")
    List<RatingsAndReviews> getCustomersWhoRatedVehicle(Vehicle vehicle);
所以我想得到的是客户对特定数量明星的评分。例如,有多少3个注视评级,2个注视评级。。。等等

未设置“nativeQuery=true”的查询将是:

 @Query("SELECT COUNT(cc.customer) AS tot FROM (SELECT DISTINCT new RatingsAndReviews(c.customer,c.customerRating)  FROM RatingsAndReviews AS c WHERE c.vehicle=?1) AS cc WHERE cc.customerRating=?2")
    Integer getNoOfRatingsForStars(Vehicle vehicle,double star);
这将产生以下错误

java.lang.IllegalArgumentException:方法公共抽象java.lang.Integer的查询验证失败


如果我对查询所做的操作是错误的,请纠正我,并感谢任何排序帮助。

首先在db查询客户端手动测试查询语法:例如

SELECT COUNT(cc.customer) AS tot
  FROM
    SELECT DISTINCT new RatingsAndReviews(c.customer,c.customerRating)
    FROM RatingsAndReviews AS c
    WHERE c.vehicle='some test value here' AS cc
  WHERE cc.customerRating=<some test value here>
选择计数(抄送客户)作为总计
从…起
选择不同的新评级和评论(c.客户,c.客户勘误)
从评级和评论到c
式中,c.vehicle='此处的一些测试值'作为cc
其中cc.customerRating=

上面有许多语法问题,例如,您不能使用
new Type()
,应该引用表名而不是类名。

车辆
类型的定义是什么。这可能无法正确转换为本机查询。在调用JPA方法之前,通过调用
.toString()
(或
.name()
,如果是枚举)尝试将其设置为
字符串类型。@karmakaze,我没有真正遵循或理解您上面的建议。但是,上面编辑了查询工作件的输出。使用
nativeQuery=true
时的查询语言(引号中@query的一部分)与未使用
nativeQuery=true
时的格式不同。使用
nativeQuery=true
时,格式应与直接使用数据库客户端输入命令时使用的SQL语法相同。唯一的特殊区别是参数占位符
?1
?2
等。当未使用
nativeQuery=true
时,该格式称为JPQL,介于SQL和Java之间。因此,第二个工作查询中的所有JPQL部分都必须转换为SQL,以便与
nativeQuery
@karmakaze一起使用,非常感谢您提供的提示。我可以知道是否可以在不使用
nativeQuery=true
@karmakaze的情况下编写带有子网的
select语句
,当我在数据库中测试我的查询并在程序中使用它时,我能够使用
nativeQuery=true
得到结果。但我想知道如何在不使用
nativeQuery=true
的情况下生成相同的结果。
SELECT COUNT(cc.customer) AS tot
  FROM
    SELECT DISTINCT new RatingsAndReviews(c.customer,c.customerRating)
    FROM RatingsAndReviews AS c
    WHERE c.vehicle='some test value here' AS cc
  WHERE cc.customerRating=<some test value here>