此代码为我提供了两个日期时间和计数的列表。但我需要按时间分组,没有时间。
谁能给我一个线索怎么做?谢谢!
Criteria criteria = getSession().createCriteria(Parcel.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("dateCreated"));
projectionList.add(Projections.rowCount());
criteria.setProjection(projectionList);
List<Object[]> results = criteria.list();
结果:
2013-04-27 11:08:00.0 | 32
2013-04-27 11:10:00.0 | 5
2013-04-28 15:03:27.0 | 12
我需要:
2013-04-27 | 37
2013-04-28 | 12
答案 0 :(得分:13)
您可能会发现Projections#sqlGroupProjection方法很有用。它允许您使用从date()
列中提取Date
的SQL函数DateTime
。基本上,而不是调用
projectionList.add(Projections.groupProperty("dateCreated"));
使用
projectionList.add(Projections.sqlGroupProjection("date(dateCreated) as createdDate", "createdDate", new String[] { "createdDate" }, new Type[] { StandardBasicTypes.DATE }));