This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate Count Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Count Query" Watch "Hibernate Count Query" New topic

Hibernate Count Query

joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

Here q1 and q2 don't behave the same (which is strange as its been interchangeably used in the hibernate reference document).

The query q1 works fine for tables where one has 'single column' primary key, but for tables having composite keys this breaks as follows:

Hibernate: select count((employee0_.NAME, employee0_.AGE)) as col_0_0_ from Employee employee0_ [Here 'Name' and 'Age' form the composite key for Employee]
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: java.sql.SQLException: ORA-00907: missing right parenthesis

Looks like for q1 it counts the primary key, which will definitely fail in case of composite key.

Hope this helps

Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
hi joy,

Thanks for sharing.
that was informative.

I agree. Here's the link:
subject: Hibernate Count Query
jQuery in Action, 3rd edition