HQL如何在实体中使用sum函数

时间:2016-06-09 09:21:36

标签: java hibernate hql

大家好,我需要你的帮助;

如何将HQL的Sum函数用于实体OneToMany的列表。

我尝试通过代码示例解释我的问题。

这是我的发票实体

@Entity
public class Invoice extends ExtendedBaseEntity {

@Column(nullable = true,length = 50)
private String invoiceNo;

@Column(nullable = true)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", timezone = "EET")
private Date invoiceDate;

@Column(nullable = true,precision = 10,scale = 2)
private BigDecimal amount; getters and setters.....

这是我的文件实体。这个实体持有发票清单OneTOMany

@Entity    
public class File extends ExtendedBaseEntity {
@Column(nullable = true, length = 50)
private String name;

@Column(nullable = true, length = 50)
private String surname;

@Column(nullable = true, length = 50)
private String lawyerCode;

@Column(nullable = true, length = 50)
private String sellerCode;

@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "MBSFILE_OID")
private List<Invoice> invoices = new ArrayList<Invoice>(); setters and getters ....

这里是我的hql,我想要Sum(invoice.amount)。但是查询没有成功,我得到了错误的结果。

SELECT DISTINCT
            SUM(invoice.amount) AS totalAmount
FROM file 
LEFT JOIN file.invoices AS invoice

我该如何应对?你能帮帮我吗?

0 个答案:

没有答案