COUNT operator with composite primary key and JPA 2.0 Criteria API produces bad sql

Description

Hibernate produces a invalid SQL if anyone use the count() operator with the JPA 2.0 Criteria API. Code:

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class);
Root<EntityCompositePK> r = cq.from(EntityCompositePK.class);

cq.multiselect(cb.count(r));
assertEquals(0, ((Long) (getEntityManager().createQuery(cq).getSingleResult())[0]).intValue());

I'm sending a little maven project which reproduces the bug.

Environment

hibernate-entitymanager 3.6.0.Beta1, glassfish 3.0.1, linux ubuntu 10.04, MS SQL Server 2008

Activity

Show:
Steve Ebersole
March 8, 2012, 4:09 PM

Well we still need to make sure it works for you with your db in SOME release of Hibernate. Otherwise JBoss has nothing to pull in. What Dialect do you use?

Jeremy Whiting
March 8, 2012, 6:25 PM

We have been using the MySQL InnoDB dialect.

Ståle W. Pedersen
March 12, 2012, 3:24 PM

hi, i created a test case that confirm that count query with EmbeddedId use all Id's in the argument. the test also do a count for a class using ClassId which create a sql that only use one Id.
I havent figured out how to fetch the sql to the test, so you have to view the log to confirm atm.
https://github.com/stalep/hibernate-orm/commit/54911765de7981ec9a2318fc9928be68ff37bab3

Ståle W. Pedersen
March 12, 2012, 4:00 PM

i created a jira issue on as7 here; https://issues.jboss.org/browse/AS7-4135

Brett Meyer
March 7, 2014, 10:09 PM

Bulk closing tickets resolved in released versions

Assignee

JohnV

Reporter

Fabio Oliveira

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

Priority

Minor
Configure