Skip to content

Commit

Permalink
HHH-7165 - count() query on classes using EmbeddedId should not use i…
Browse files Browse the repository at this point in the history
…d column tuple on Dialects which dont support non-distinct tuple counts
  • Loading branch information
sebersole committed Mar 13, 2012
1 parent c943285 commit 41dd707
Showing 1 changed file with 28 additions and 1 deletion.
Expand Up @@ -24,6 +24,7 @@
package org.hibernate.test.cid;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand All @@ -32,6 +33,9 @@
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.engine.query.spi.HQLQueryPlan;
import org.hibernate.hql.spi.QueryTranslator;

import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

import static org.junit.Assert.assertEquals;
Expand All @@ -47,6 +51,29 @@ public String[] getMappings() {
return new String[] { "cid/Customer.hbm.xml", "cid/Order.hbm.xml", "cid/LineItem.hbm.xml", "cid/Product.hbm.xml" };
}

@Test
public void testNonDistinctCountOfEntityWithCompositeId() {
// the check here is all based on whether we had commas in the expressions inside the count
final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan(
"select count(o) from Order o",
false,
Collections.EMPTY_MAP
);
assertEquals( 1, plan.getTranslators().length );
final QueryTranslator translator = plan.getTranslators()[0];
final String generatedSql = translator.getSQLString();

final int countExpressionListStart = generatedSql.indexOf( "count(" );
final int countExpressionListEnd = generatedSql.indexOf( ")", countExpressionListStart );
final String countExpressionFragment = generatedSql.substring( countExpressionListStart+6, countExpressionListEnd+1 );
final boolean hadCommas = countExpressionFragment.contains( "," );

// set up the expectation based on Dialect...
final boolean expectCommas = sessionFactory().getDialect().supportsTupleCounts();

assertEquals( expectCommas, hadCommas );
}

@Test
public void testQuery() {
Session s = openSession();
Expand All @@ -55,7 +82,7 @@ public void testQuery() {
t.commit();
s.close();
}

@Test
public void testCompositeIds() {
Session s = openSession();
Expand Down

0 comments on commit 41dd707

Please sign in to comment.