Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.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
Java 未能在JPA查询中进行投影_Java_Sql_Spring_Jpa_Spring Data Jpa - Fatal编程技术网

Java 未能在JPA查询中进行投影

Java 未能在JPA查询中进行投影,java,sql,spring,jpa,spring-data-jpa,Java,Sql,Spring,Jpa,Spring Data Jpa,很抱歉,SQL查询很混乱 我有这个数据结构 @Entity public class Stock { //Composite PK? @Id @NotNull private String id; @NotNull private String product_id; @NotNull private Integer quantity; @NotNull private LocalDateTime timestamp; 及 这两个类都具有适当的构造函数 我有相应的库存存储库 public i

很抱歉,SQL查询很混乱

我有这个数据结构

@Entity
public class Stock {

//Composite PK?
@Id
@NotNull
private String id;
@NotNull
private String product_id;
@NotNull
private Integer quantity;
@NotNull
private LocalDateTime timestamp;

这两个类都具有适当的构造函数

我有相应的库存存储库

public interface StockRepository extends JpaRepository<Stock, String> {
我试图做的是一个复杂的SQL查询,注释更容易可视化查询,它应该返回一个ProductSeld而不是Stock的列表

 /*
SELECT (t1.quantity - t2.quantity) as itemsSold, t1.product_id as pd
FROM stock t1 CROSS JOIN
 stock t2
WHERE MONTH(t1.timestamp) = 8 AND DAY(t1.timestamp) = 26
AND MONTH(t2.timestamp) = 8 AND DAY(t2.timestamp) = 27
AND t1.product_id = t2.product_id
ORDER BY itemsSold DESC
LIMIT 3;
 */

//Not working, not too sure why, says it can't find column quantity. In my view it should be working since it's a valid SQL Query.
@Query(value = "SELECT  new com.stock.stock.model.ProductSold(t1.product_id as productId, (t1.quantity - t2.quantity) as itemsSold )" +
        "    FROM Stock t1 CROSS JOIN Stock t2" +
        "    WHERE MONTH(t1.timestamp) = ?1 AND DAY(t1.timestamp) = ?2" +
        "    AND MONTH(t2.timestamp) = ?3 AND DAY(t2.timestamp) = ?4" +
        "    AND t1.product_id = t2.product_id" +
        "    ORDER BY itemsSold DESC" +
        "    LIMIT 3", nativeQuery = true)
List<ProductSold> findItemsSoldByTimestamp(int month1, int day1, int month2, int day2);
但我明白了:

org.h2.jdbc.JdbcSQLSyntaxErrorException:SQL语句中的语法错误选择新COM。[*]STOCK.STOCK.MODEL.PRODUCTSOLDT1.PRODUCT_ID作为PRODUCTID,T1.QUANTITY-T2.QUANTITY作为ITEMSSOLD


你知道怎么了吗?我尝试在SQL表达式中不进行构造,但我得到了一个ConverterNotFoundException

您使用的构造函数表达式仅在JPQL中可用,但您也将您的查询标记为本机查询,即不是本机查询的SQL。

我认为它不是本机查询,但如果我去掉本机查询,它不允许我在查询中创建对象。但是如果我将本机查询取出,它不允许我在查询中创建对象。相反,只有当它不是本机查询时,才可以使用构造函数表达式。当然,接下来必须修复查询的其余部分,例如删除JPQL不支持的limit子句。
 /*
SELECT (t1.quantity - t2.quantity) as itemsSold, t1.product_id as pd
FROM stock t1 CROSS JOIN
 stock t2
WHERE MONTH(t1.timestamp) = 8 AND DAY(t1.timestamp) = 26
AND MONTH(t2.timestamp) = 8 AND DAY(t2.timestamp) = 27
AND t1.product_id = t2.product_id
ORDER BY itemsSold DESC
LIMIT 3;
 */

//Not working, not too sure why, says it can't find column quantity. In my view it should be working since it's a valid SQL Query.
@Query(value = "SELECT  new com.stock.stock.model.ProductSold(t1.product_id as productId, (t1.quantity - t2.quantity) as itemsSold )" +
        "    FROM Stock t1 CROSS JOIN Stock t2" +
        "    WHERE MONTH(t1.timestamp) = ?1 AND DAY(t1.timestamp) = ?2" +
        "    AND MONTH(t2.timestamp) = ?3 AND DAY(t2.timestamp) = ?4" +
        "    AND t1.product_id = t2.product_id" +
        "    ORDER BY itemsSold DESC" +
        "    LIMIT 3", nativeQuery = true)
List<ProductSold> findItemsSoldByTimestamp(int month1, int day1, int month2, int day2);