使用JPA查询从列表中进行选择

时间:2014-08-26 03:32:20

标签: java jpa

我是JPA的新手,并定义了以下实体

public class CustomerOrders {
  @Column(name = "CUSTOMER_ID", nullable = false)
  private Long customerId;
  @OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true)
  @JoinColumn(name = "ORDER_ID", nullable = false)
  private List<Order> orders;
}

public class Order {
  @Column(name = "ORDER_ID", nullable = false)
  private Long orderId;
  @Column(name = "AMOUNT", precision = 16, scale = 2, nullable = false)
  private BigDecimal orderAmount;
  @Column(name ="AWARD_FULFILLED_INDIC", nullable = false)
  private Boolean awardFulIndicator;
}

鉴于上述情况,我将如何针对CustomerOrders实体编写JPA查询,当将客户ID作为参数传递时,它将返回具有至少一个订单的CustomerOrders,订单的金额大于$ 10正数且其AWARD_FULFILLED_INDIC是真的吗?

提前谢谢。

2 个答案:

答案 0 :(得分:0)

我试试这个:

SELECT * FROM CustomerOrders c INNER JOIN c.orders o 
WHERE c.customerId = :cId AND o.awardFulIndic = true 
HAVING MAX(o.orderAmount) >= 10.00;

答案 1 :(得分:0)

@MawrCoffeePls&#39的解决方案