Hibernate:返回按日期分组的特定列的总和

时间:2014-09-03 07:38:31

标签: java hibernate criteria

我需要编写一个条件查询来检索按特定amount分组的date列的总和。我的意思是由以下列组成:

  id      partner_id    amount         date       platform_id
serial     integer    numeric(13.4)  timestamp      integer

Hibernate映射以下列方式执行:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@ManyToOne(targetEntity = Partner.class, fetch = FetchType.EAGER)
@JoinColumn(name="partner_id")
private Partner partner;

@Column(name = "amount")
private BigDecimal amount;

@Column(name = "date")
private Date date;

@ManyToOne(targetEntity = Platform.class, fetch = FetchType.EAGER)
@JoinColumn(name="platform_id")
private Platform platform;

我写了以下查询:

Criteria criteria = getSession().createCriteria(DailyProfit.class)
        .add(Restrictions.eq("partner", partner))
        .add(Restrictions.eq("platform", platform))
        .add(Restrictions.between("date", from, to));

criteria.setProjection(Projections.groupProperty("date"));
criteria.setProjection(Projections.sum("amount").as("DailySum"));

criteria.list()会返回[1119950.8300, null, null, null, null, null, null, null, null, null]。这绝对不是我所期待的。你能解释一下这个结果并帮我修改标准查询吗?

1 个答案:

答案 0 :(得分:0)

您的代码非常完美

criteria.list() - 将返回对象列表(集合)。您应该使用index或iterator来获取result的值。

整数金额=(整数)criteria.uniqueResult();