title: How to group by date in QueryDSL?
body: <p>I've got following JPA entity:</p>
  
  <pre><code>@Table(name = "execute")
  public class ScenarioExecution {
      .
      .
      .
      @Column(name = "date")
      @Temporal(TemporalType.TIMESTAMP)
      private Date date;
      .
      .
      .
  }
  </code></pre>
  
  <p>where <code>date</code> is stored as <code>TIMESTAMP</code>, so it includes date and time. What I want is to find all dates, but rounded to whole days, in SQL:</p>
  
  <pre><code>SELECT CAST(e.DATE AS DATE) as day FROM EXECUTE e GROUP BY day
  </code></pre>
  
  <p>How can I translate it to QueryDSL? I've tried:</p>
  
  <pre><code>JPAQuery query = new JPAQuery(entityManager);
  QScenarioExecution executions = QScenarioExecution.scenarioExecution;
  DateExpression&lt;Date&gt; alias = DateTemplate.create(Date.class, "CAST({0} as DATE)", executions.date).as("day");
      return query.from(executions)
              .groupBy(alias)
              .list(alias);
  </code></pre>
  
  <p>but I've got exception <code>java.lang.IllegalStateException: scenarioExecution is already used</code>.
  I'm using H2 as database backend.</p>
  
  <p><strong>EDIT:</strong>
  Full stacktrace:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>I've changed the method a bit. Now it looks like:</p>
  
  <pre><code>public List&lt;Date&gt; getExecutionDates() {
      EntityManager entityManager = createEntityManager();
      JPAQuery query = new JPAQuery(entityManager);
      QScenarioExecution executions = QScenarioExecution.scenarioExecution;
      try {
          DateExpression&lt;Date&gt; alias = DateTemplate.create(Date.class, "CAST({0} as DATE)", executions.date);
          return query.from(executions)
                  .groupBy(alias)
                  .list(alias);
      } finally {
          entityManager.close();
      }
  }
  </code></pre>
  
  <p>But now it throws such exception:</p>
  
  <pre><code>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.&lt;init&gt;(HQLQueryPlan.java:126)
      at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(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)
  </code></pre>