按日分组,不适用于PostgreSQL(QueryDSL,joda.DateTime)

时间:2018-07-30 10:17:00

标签: spring postgresql hsqldb querydsl dayofweek

我正在从事一个涉及SpringBoot 1.5.8.RELEASE,JPA,QueryDSL 4.1.4和HSQLDB 2.3.5的项目。 我目前正在从HSQLDB切换到PostgreSQL 9.1-901-1.jdbc4。查询中存在一些错误,但是我能够解决它们。但是,我无法修复其中的一个。

我有一组实体,标有时间戳(joda.DateTime)。我想按dayOfWeek对它们进行分组并获得计数。查询看起来像这样:

JPAQuery<Tuple> query = new JPAQuery<Tuple>(entityManager);
query.select(entity.time.dayOfWeek(), entity.count()).groupBy(entity.time.dayOfWeek());
query.where(...);
query.from(entity);

这以前在HSQLDB上可以正常使用,但在PostgreSQL上不再可用。错误看起来像这样(我用entity替换了实际的实体名称):

2018-07-30 11:41:35,314 1892699 ERROR o.n.r.b.r.RestResponseEntityExceptionHandler - Request (ServletWebRequest: uri=***) could not be handled. RuntimeException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
+-[METHOD_CALL] MethodNode: '('
|  +-[METHOD_NAME] IdentNode: 'dayofweek' {originalText=dayofweek}
|  \-[EXPR_LIST] SqlNode: 'exprList'
|     \-[DOT] DotNode: 'entity0_.time' {propertyName=time,dereferenceType=PRIMITIVE,getPropertyPath=time,path=entity.time,tableAlias=entity0_,className=.....Entity,classAlias=entity}
[...]

上面列出的查询按月分组,例如

query.select(entity.time.month(), entity.count()).groupBy(entity.time.month());

工作正常。

生成的SQL如下所示:

select dayofweek(entity.time), count(entity)
from Entity entity
where ...
group by dayofweek(entity.time)

QueryDSL,DateTime和JPA是否有问题?据我所知,PostgreSQL期望星期几的关键字是dow而不是dayofweek。 该如何解决?

编辑:按week分组同样适用。

0 个答案:

没有答案