JPA命名查询

时间:2012-12-16 12:33:47

标签: java sql jpa

嘿,我是JPA的新手,对SQL很新,我需要写一个选择查询

我需要:

“选择金额超过所有美元付款平均价值的所有付款”

我有一个JPA实体:

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String account;
private double amount;
private String currency;

我尝试过:

@NamedQuery(name="payByUSD" , query="SELECT x FROM Payment x WHERE x.amount > (SELECT AVG(x.amount)from Payment)")

但我收到以下错误:

- The FROM clause must defined at least one identification variable declaration.
- The right expression is missing from the arithmetic expression.

任何人都可以指出我正确的方向。

1 个答案:

答案 0 :(得分:5)

使用

SELECT x FROM Payment x 
WHERE x.amount > (SELECT AVG(p.amount) from Payment p)

你的子查询是

SELECT AVG(x.amount) from Payment

,并且未定义x