wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Group by desc in jpql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Group by desc in jpql" Watch "Group by desc in jpql" New topic
Author

Group by desc in jpql

Sharath Upadhya
Greenhorn

Joined: Dec 01, 2009
Posts: 9
I am having issues in using the 'group by' clause in JPQL.

I need to fetch all the records grouped by their parent records, but need to group by the latest record.(grouping in reverse order)

I tried running 'select * from table_name group by parent_record desc' on mysql and there was no problem.

but when I tried it with JPQL, it started giving exceptions.

please guys, help me if anyone of you have tried this....

thanks in advance.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

What is your jpql query?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Sharath Upadhya
Greenhorn

Joined: Dec 01, 2009
Posts: 9
thanks for responding. here's my JPQL query.

SELECT x FROM Discussions AS x WHERE x.id IN (SELECT y.id FROM Discussions AS y WHERE y.parentDiscussion IS NOT NULL GROUP BY y.parentDiscussion DESC);

This is just a simplified query. if I remove 'DESC', i am getting the output. but not the expected one.

without DESC, this query fetches only the first comment for any discussion. my requirement is to fetch only the latest comments for all the discussions.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why are you using a sub-query in your jpql query while you're not using a sub-query in your native query?
Sharath Upadhya
Greenhorn

Joined: Dec 01, 2009
Posts: 9
I am using multiple sub-queries in both queries.

but I haven't posted those queries just for simplicity. and to give you an idea of what am trying to accomplish.

and it cannot be done without sub-queries as you know it.
 
 
subject: Group by desc in jpql