JPA规范复杂查询

时间:2016-04-01 03:49:16

标签: java spring hibernate jpa spring-data

我有以下数据: -

Product Reason Qty
Pepsi   IN     10
Pepsi   Out    2
Pepsi   In     15
Pepsi   Out    5 
Coke    IN     100
Coke    Out    20
Coke    In     35
Coke    Out    25

从JPA规范我想在下面执行以下查询: -

select * from (
(select SUM(QTY) from stock where reason like 'IN') as INDATA -
(select SUM(QTY) from stock where reason like 'OUT') as OUTDATA

) as TBL;

结果如下: -

Product Qty
Pepsi   18
Coke    90

我第一次使用JPA规范,我不知道如何执行查询以从库存表中获取当前库存。

1 个答案:

答案 0 :(得分:0)

假设您有一个',比如Entity,则会将上述表Beverage映射为:{/ p>

Stock

您可以使用@Entity @Table(name="Stock") class Beverage { public Beverage(String brand, String reason, int quantity) { this.brand = brand; this.reason = reason; this.quantity = quantity; } public Beverage() {} @Id @GeneratedValue Integer id; String brand; String reason; int quantity; } 下面的case statement along with sum

JPQL