我有一个实体,需要获得一个日期列表,无论时间如何。我如何按日期分组?
@Entity
public class RssFeedEntry {
//...
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date publishedDate;
//...
}
像这样的查询会引发异常。
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());
堆栈跟踪:
Caused by: org.h2.jdbc.JdbcSQLException: Column "RSSFEEDENT0_.PUBLISHEDDATE" must be in the GROUP BY list; SQL statement:
感谢。
答案 0 :(得分:2)
据我所知,这是数据库限制。 选择子句中的所有列必须位于 group by 子句中,除非它仅用于聚合函数(即max(),sum()等)。如果您使用的是MySQL,则情况并非如此,您就不会遇到这种问题。 MySQL是唯一一个我知道的数据库,它不能阻止你回到这个问题。
试试这个:
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());
修改强>
但这会更清晰:
List<Tuple> list = query.
from(feedEntry).
groupBy(feedEntry.publishedDate).
list(feedEntry.publishedDate);