如何在QueryDSL中按日期分组?

时间:2014-08-12 12:20:08

标签: java sql jpa querydsl

我有以下JPA实体:

@Table(name = "execute")
public class ScenarioExecution {
    .
    .
    .
    @Column(name = "date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;
    .
    .
    .
}

其中date存储为TIMESTAMP,因此包含日期和时间。我想要的是在SQL中查找所有日期,但四舍五入到整天:

SELECT CAST(e.DATE AS DATE) as day FROM EXECUTE e GROUP BY day

如何将其翻译为QueryDSL?我试过了:

JPAQuery query = new JPAQuery(entityManager);
QScenarioExecution executions = QScenarioExecution.scenarioExecution;
DateExpression<Date> alias = DateTemplate.create(Date.class, "CAST({0} as DATE)", executions.date).as("day");
    return query.from(executions)
            .groupBy(alias)
            .list(alias);

但我有异常java.lang.IllegalStateException: scenarioExecution is already used。 我正在使用H2作为数据库后端。

修改 完整的堆栈跟踪:

java.lang.IllegalStateException: scenarioExecution is already used
    at com.mysema.query.DefaultQueryMetadata.addJoin(DefaultQueryMetadata.java:172)
    at com.mysema.query.support.QueryMixin.from(QueryMixin.java:162)
    at com.mysema.query.jpa.JPAQueryBase.from(JPAQueryBase.java:84)
    at {cut}.dao.ScenarionExecutionDAO.getExecutionDates(ScenarionExecutionDAO.java:105)
    at {cut}.dao.SearchingTest.findExecutionDates(SearchingTest.java:60)

编辑2:

我已经改变了一点方法。现在看起来像:

public List<Date> getExecutionDates() {
    EntityManager entityManager = createEntityManager();
    JPAQuery query = new JPAQuery(entityManager);
    QScenarioExecution executions = QScenarioExecution.scenarioExecution;
    try {
        DateExpression<Date> alias = DateTemplate.create(Date.class, "CAST({0} as DATE)", executions.date);
        return query.from(executions)
                .groupBy(alias)
                .list(alias);
    } finally {
        entityManager.close();
    }
}

但是现在它抛出了这样的异常:

java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'CAST' {originalText=CAST}
    \-[EXPR_LIST] SqlNode: 'exprList'
       +-[DOT] DotNode: 'scenarioex0_.date' {propertyName=date,dereferenceType=PRIMITIVE,getPropertyPath=date,path=scenarioExecution.date,tableAlias=scenarioex0_,className={cut}.db.monitorLocal.model.ScenarioExecution,classAlias=scenarioExecution}
       |  +-[ALIAS_REF] IdentNode: 'scenarioex0_.id' {alias=scenarioExecution, className={cut}.db.monitorLocal.model.ScenarioExecution, tableAlias=scenarioex0_}
       |  \-[IDENT] IdentNode: 'date' {originalText=date}
       \-[IDENT] IdentNode: 'DATE' {originalText=DATE}
    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:174)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:923)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:691)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:663)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:128)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:97)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.list(AbstractJPAQuery.java:241)
    at {cut}.monitorLocal.dao.ScenarionExecutionDAO.getExecutionDates(ScenarionExecutionDAO.java:109)
    at {cut}.storage.db.monitorLocal.dao.SearchingTest.findExecutionDates(SearchingTest.java:61)

1 个答案:

答案 0 :(得分:1)

你确定两次不打电话吗?另外一天路径需要是groupBy的参数,而不是完整的别名表达式。