[英]How do I group by date with QueryDsl?
I have an entity and need to get a list of dates irrespective of the time. 我有一个实体,需要获取与时间无关的日期列表。 How do I group by dates?
如何按日期分组?
@Entity
public class RssFeedEntry {
//...
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date publishedDate;
//...
}
The query like this throws an exception. 这样的查询将引发异常。
JPAQuery query = new JPAQuery(entityManager);
List<Tuple> list = query.
from(feedEntry).
groupBy(feedEntry.publishedDate.dayOfYear(), feedEntry.publishedDate.year()).
list(feedEntry.publishedDate.dayOfMonth(),
feedEntry.publishedDate.month(),
feedEntry.publishedDate.year());
Stacktrace: 堆栈跟踪:
Caused by: org.h2.jdbc.JdbcSQLException: Column "RSSFEEDENT0_.PUBLISHEDDATE" must be in the GROUP BY list; SQL statement:
Thanks. 谢谢。
As far as I know, it's database restriction. 据我所知,这是数据库限制。 All columns in the select clause must be in the group by clause unless it's used only in an aggregate function (ie max(), sum() etc.).
除非仅在聚合函数(即max(),sum()等)中使用,否则select子句中的所有列都必须在group by子句中。 If you are using MySQL, which is not the case, you would not have this kind of problem.
如果使用的不是MySQL,则不会出现这种问题。 MySQL is the only DB, that I am aware of, which doesn't hold you back in this matter.
据我所知,MySQL是唯一的数据库,在这个问题上,它不会使您退缩。
Try this: 尝试这个:
List<Tuple> list = query.
from(feedEntry).
groupBy(feedEntry.publishedDate.dayOfMonth(),
feedEntry.publishedDate.month(),
feedEntry.publishedDate.year()).
list(feedEntry.publishedDate.dayOfMonth(),
feedEntry.publishedDate.month(),
feedEntry.publishedDate.year());
EDIT: 编辑:
That would be more legible, though: 不过,这将更加清晰:
List<Tuple> list = query.
from(feedEntry).
groupBy(feedEntry.publishedDate).
list(feedEntry.publishedDate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.