如何通过JPQL对@ElementCollection中的数据进行分组或汇总?

时间:2013-11-24 03:49:32

标签: java spring java-ee jpa ejb

我有一个班级拥有从交易金额到交易金额的多对多关系

Class Deal{
.....

private String name;

private String department;

private DealType type;

@AttributeOverrides({
    @AttributeOverride(name="value.in.available", column=@Column(name="in_avl")),
    @AttributeOverride(name="value.in.unavailable", column=@Column(name="in_unv")),
    @AttributeOverride(name="value.out.available", column=@Column(name="out_avl")),
    @AttributeOverride(name="value.out.unavailable", column=@Column(name="out_unv"))
})
@ElementCollection(fetch = FetchType.EAGER)
private Map<Transaction, AmountBucket> transactionAmounts= new TreeMap<Coffer, AmountBucket>();
}

然后实体AmountBucket是这样的:

Class AmountBucket{
    private Amount in;
    private Amount out;
}

Class Amount{
    private BigDecimal available;
    private BigDecimal unavaiable;
}

但是对于@ElementCollection目的,树层次结构使用@AttributeOverride存储在单个表中。

此处的目标是总结transactionAmounts而不管DealType。如果它没有选择可爱的@ElementCollection功能,那么我们可以在JPQL然后组中进行内连接,但是当我尝试使用带有ElementCollection结构的JPQL对这些东西进行分组时,存在一个问题:< / p>

Failed to execute query 
"SELECT NEW DEAL(d.name, d.department, SUM(d.transactionAmounts)) FROM 
Deal AS d GROUP BY d.name, d.department". 
Check the query syntax for correctness. See nested exception for details.; nested 
exception is <openjpa-2.2.0-r422266:1244990 nonfatal user error>    org.apache.openjpa.persistence.ArgumentException: Failed to execute query ......

可能d.transactionAmounts无法归结为@CollectionElement?我有一个用于此JPQL查询的构造函数:

public Deal(String name, String department, Map<Transaction, AmountBucket> transactionAmounts) {
    this.name= name;
    this.department= department;
    this.transactionAmounts= transactionAmounts;
    this.type = DealType.SUM;
}

我真的没有在JPQL中看到任何语法问题,也没有看到任何关于如何分组的文档,总结了@ElementCollection中的值。请帮助谢谢。

1 个答案:

答案 0 :(得分:0)

问题似乎在构造函数中:它没有正确的签名。它应该有以下一个:

public Deal(String name, String department, long transactionAmount);//you might try also with int instead of long

因为SUM(d.transactionAmounts)会返回一个数字,而不是构造函数中的MAP