Hi timo,
We changed our code according your suggestion:
QTable1 t1 = QTable1.table1;
QTable2 t2 = QTable2.table2;
JPAQuery query2 = new JPAQuery(entityManager);
JPASubQuery sub = new JPASubQuery();
PathBuilder<Object[]> total = new PathBuilder<Object[]>(Object[].class, "total");
List<Tuple> rows = query2.from(t1)
.join(sub.from(t2).where(t2.date.between(dateInicial, dateFinal), t2.keyword.eq(keyword))
.groupBy(t2.customerId)
.list(t2.customerId, t2.date.max(), t2.receita.sum().as("receita")),
total
).with(t1.date.eq(t2.date), t1.id.eq(t2.customerId))
.orderBy(
t1.id)
.list(
t1.id, t1.campaign, total.get("receita"));
But now we are having the following error:
16:10:53,123 ERROR [org.hibernate.hql.internal.ast.ErrorCounter] (http-localhost-127.0.0.1-8080-6) line 3:56: unexpected token: max
16:10:53,123 ERROR [org.hibernate.hql.internal.ast.ErrorCounter] (http-localhost-127.0.0.1-8080-6) line 3:56: unexpected token: max: line 3:56: unexpected token: max
We are using hibernate 4.1.3.Final with JBoss AS 7.1.1 and the following configuration in applicationContext.xml and persistence.xml:
jpaVendorAdapter = org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
jpaDialect = org.springframework.orm.jpa.vendor.HibernateJpaDialect
databasePlatform = org.hibernate.dialect.SQLServerDialect
database = SQL_SERVER
The generated query is:
select
table1.id, table1.campaign, total.receita
from Table1 table1
join (select table2.campaignId as campaignId, max(table2.date) as date, sum(table2.receita) as receita
from Table2 table2
where table2.date between '2012-06-24' and '2012-06-25' and table2.keyword = 'tv'
group by table2.campaignId) as total
with table1.date = total.date and
table1.id = total.campaignId
order by
table1.id asc
I tried to put the generated query in a sql server client and i got this error:
Erro:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 319, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
I believe that SQL Server isn't recognizing "with" as a valid clause to use with join, we are using SQL Server 2005.
Do you know why we are getting this error?? Is this the expected query?
Thanks in advance,
Danilo.