Criteria 2 sum Ljava.lang.Object;不能演员

时间:2018-02-05 09:26:04

标签: java sql hibernate sum criteria

我正在尝试使用带有2 sum的Criteria来构建请求但是出现此错误:

  

java.lang.ClassCastException:[Ljava.lang.Object;无法施展   统计

统计类:

@Entity
@Table(name = "STATS")
@Getter
public class Stats{

    @Id
    @Column(name = "STAT_ID")
    private int statId;

    @Column(name = "NB_STATS_CONFIRMED")
    private long nbStatsConfirmed;

    @Column(name = "NB_STATS_DELETED")
    private long nbStatsDeleted;

    @Column(name = "DATE_STAT")
    private Date dateStat;

}

看起来像:

STAT_ID     NB_STATS_CONFIRMED     NB_STATS_DELETED     DATE_STAT
  1                5                     2            2018-01-23
  2                2                     1            2018-01-22
  3                8                     0            2018-01-20

我想在两个日期(DATE_STAT)之间总结NB_STATS_CONFIRMED和NB_STATS_DELETED。

标准要求:

   @SuppressWarnings("unchecked")
    @Override
    public List<Stats> listStats(final Date start, final Date end) {
        Criteria cr = createRequest(); // method creating a criteria

        if (start != null) {
            cr.add(Restrictions.ge("dateStat", start));
        }
        if (fin != null) {
            cr.add(Restrictions.le("dateStat", end));
        }
        cr.setProjection(Projections.projectionList()
                .add(Projections.sum("nbStatsConfirmed"), "nbStatsConfirmed")
                .add(Projections.sum("nbStatsCanceled"), "nbStatsCanceled"));

        return cr.list();
    }

然后我称之为方法:

List<Stats> listStats = dao.listStats(start, end);

我试过了[Ljava.lang.Object; cannot be cast to&amp; Sum projection and results constraint

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,如果有人感兴趣但必须做2个请求:

long nbStatsConfirmed = (long) AJobUtil.valeurDefaut((cr.setProjection(
        Projections.sum("nbStatsConfirmed"))
        .uniqueResult()), (long) 0);

long nbStatsCanceled = (long) AJobUtil.valeurDefaut((cr.setProjection(
        Projections.sum("nbStatsCanceled"))
        .uniqueResult()), (long) 0);

long[] stats = { nbStatsConfirmed, nbStatsCanceled };

return stats;

如果你有不同的解决方案(有1个请求),仍然可以打开!