按年份或周分组时出现IllegalStateException

时间:2015-07-02 14:50:18

标签: java oracle jpa querydsl

我有一个QueryDSL JPAQuery,应按日历周分组。在调用list(...)时遇到异常。只要我用year()替换yearWeek()的所有出现就可以了。

java代码:

QCrmContact qCrmContact = QCrmContact.crmContact;
BooleanExpression whereClause = qCrmContact.beUserId.eq(beUserId).and(qCrmContact.crmContactTypeId.in(contactTypeIds));

JPAQuery query = new JPAQuery(entityManager);

query.from(qCrmContact).where(whereClause).orderBy(qCrmContact.createdDatetime.yearWeek().asc())
     .groupBy(qCrmContact.createdDatetime.yearWeek(), qCrmContact.crmContactTypeId);
List<Tuple> rows = query.list(qCrmContact.createdDatetime.yearWeek(), qCrmContact.crmContactTypeId, qCrmContact.companyId.countDistinct());

例外:

Exception occurred during processing request: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
 +-[METHOD_CALL] MethodNode: '('
 |  +-[METHOD_NAME] IdentNode: 'yearweek' {originalText=yearweek}
 |  \-[EXPR_LIST] SqlNode: 'exprList'
 |     \-[DOT] DotNode: 'crmcontact0_.CREATED_DATETIME' {propertyName=createdDatetime,dereferenceType=ALL,propertyPath=createdDatetime,path=crmContact.createdDatetime,tableAlias=crmcontact0_,className=de.xxx.cxlbackend.database.model.CrmContact,classAlias=crmContact}
 |        +-[ALIAS_REF] IdentNode: 'crmcontact0_.ID' {alias=crmContact, className=de.xxx.cxlbackend.database.model.CrmContact, tableAlias=crmcontact0_}
 |        \-[IDENT] IdentNode: 'createdDatetime' {originalText=createdDatetime}

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
 +-[METHOD_CALL] MethodNode: '('
 |  +-[METHOD_NAME] IdentNode: 'yearweek' {originalText=yearweek}
 |  \-[EXPR_LIST] SqlNode: 'exprList'
 |     \-[DOT] DotNode: 'crmcontact0_.CREATED_DATETIME' {propertyName=createdDatetime,dereferenceType=ALL,propertyPath=createdDatetime,path=crmContact.createdDatetime,tableAlias=crmcontact0_,className=de.xxx.cxlbackend.database.model.CrmContact,classAlias=crmContact}
 |        +-[ALIAS_REF] IdentNode: 'crmcontact0_.ID' {alias=crmContact, className=de.xxx.cxlbackend.database.model.CrmContact, tableAlias=crmcontact0_}
 |        \-[IDENT] IdentNode: 'createdDatetime' {originalText=createdDatetime}

我可以使用一些hack并将原生SQL传递给Oracle:

to_char(CREATED_DATETIME, 'IW.YYYY')

1 个答案:

答案 0 :(得分:0)

通过替换

解决了这个问题
qCrmContact.createdDatetime.yearWeek()

StringExpression createdWeekYear = StringTemplate.create("to_char(CREATED_DATETIME, 'IW.YYYY')");