|
From: <hib...@li...> - 2006-05-08 17:41:38
|
Author: max...@jb...
Date: 2006-05-08 13:41:21 -0400 (Mon, 08 May 2006)
New Revision: 9907
Added:
trunk/Hibernate3/test/org/hibernate/test/hql/CriteriaHQLAlignmentTest.java
Modified:
trunk/Hibernate3/src/org/hibernate/criterion/AggregateProjection.java
trunk/Hibernate3/src/org/hibernate/criterion/AvgProjection.java
trunk/Hibernate3/src/org/hibernate/criterion/CountProjection.java
trunk/Hibernate3/src/org/hibernate/criterion/RowCountProjection.java
trunk/Hibernate3/src/org/hibernate/dialect/Dialect.java
trunk/Hibernate3/src/org/hibernate/engine/SessionFactoryImplementor.java
trunk/Hibernate3/src/org/hibernate/persister/entity/AbstractEntityPersister.java
trunk/Hibernate3/test/org/hibernate/test/TestCase.java
trunk/Hibernate3/test/org/hibernate/test/criteria/CriteriaQueryTest.java
trunk/Hibernate3/test/org/hibernate/test/hql/HQLSuite.java
trunk/Hibernate3/test/org/hibernate/test/hql/HQLTest.java
trunk/Hibernate3/test/org/hibernate/test/immutable/ImmutableTest.java
trunk/Hibernate3/test/org/hibernate/test/ops/MergeTest.java
trunk/Hibernate3/test/org/hibernate/test/ops/SaveOrUpdateTest.java
Log:
HHH-1724 Critieria needs to be aligned with new aggreation type rules.
Done by having Criterion looking up SQLFunction via the dialect (before Criteria
and HQL were seperately maintained)
+ added (optional) assertDataIsRemoved test function
+ fixed aggregation type tests.
Modified: trunk/Hibernate3/src/org/hibernate/criterion/AggregateProjection.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/criterion/AggregateProjection.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/src/org/hibernate/criterion/AggregateProjection.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -3,6 +3,8 @@
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
+import org.hibernate.dialect.function.SQLFunction;
+import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.type.Type;
/**
@@ -25,9 +27,22 @@
public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
- return new Type[] { criteriaQuery.getType(criteria, propertyName) };
+ SessionFactoryImplementor factory = criteriaQuery.getFactory();
+ Type type = criteriaQuery.getType(criteria, propertyName);
+
+ SQLFunction function = findSQLFunction( factory, aggregate );
+
+ if(function==null) {
+ return new Type[] { type };
+ } else {
+ return new Type[] { function.getReturnType( type, factory ) };
+ }
}
+ protected SQLFunction findSQLFunction(SessionFactoryImplementor sfi, String functionName) {
+ return ( SQLFunction ) sfi.getDialect().getFunctions().get( functionName.toLowerCase() );
+ }
+
public String toSqlString(Criteria criteria, int loc, CriteriaQuery criteriaQuery)
throws HibernateException {
return new StringBuffer()
Modified: trunk/Hibernate3/src/org/hibernate/criterion/AvgProjection.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/criterion/AvgProjection.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/src/org/hibernate/criterion/AvgProjection.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -1,11 +1,6 @@
//$Id$
package org.hibernate.criterion;
-import org.hibernate.Criteria;
-import org.hibernate.Hibernate;
-import org.hibernate.HibernateException;
-import org.hibernate.type.Type;
-
/**
* @author Gavin King
*/
@@ -13,10 +8,5 @@
public AvgProjection(String propertyName) {
super("avg", propertyName);
- }
-
- public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
- throws HibernateException {
- return new Type[] { Hibernate.DOUBLE };
- }
+ }
}
Modified: trunk/Hibernate3/src/org/hibernate/criterion/CountProjection.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/criterion/CountProjection.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/src/org/hibernate/criterion/CountProjection.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -26,11 +26,6 @@
}
}
- public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
- throws HibernateException {
- return new Type[] { Hibernate.INTEGER };
- }
-
public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
throws HibernateException {
StringBuffer buf = new StringBuffer();
Modified: trunk/Hibernate3/src/org/hibernate/criterion/RowCountProjection.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/criterion/RowCountProjection.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/src/org/hibernate/criterion/RowCountProjection.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -4,6 +4,8 @@
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
+import org.hibernate.dialect.function.SQLFunction;
+import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.type.Type;
/**
@@ -20,9 +22,22 @@
public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
- return new Type[] { Hibernate.INTEGER };
+ SessionFactoryImplementor factory = criteriaQuery.getFactory();
+
+ Type type = Hibernate.LONG;
+ SQLFunction function = findSQLFunction( factory, "count" );
+
+ if(function==null) {
+ return new Type[] { type };
+ } else {
+ return new Type[] { function.getReturnType( type, factory ) };
+ }
}
+ protected SQLFunction findSQLFunction(SessionFactoryImplementor sfi, String functionName) {
+ return ( SQLFunction ) sfi.getDialect().getFunctions().get( functionName.toLowerCase() );
+ }
+
public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
throws HibernateException {
return new StringBuffer()
Modified: trunk/Hibernate3/src/org/hibernate/dialect/Dialect.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/dialect/Dialect.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/src/org/hibernate/dialect/Dialect.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -62,6 +62,34 @@
private static final Map STANDARD_AGGREGATE_FUNCTIONS = new HashMap();
+ private static SQLFunction classicCount = new StandardSQLFunction("count") {
+ public Type getReturnType(Type columnType, Mapping mapping) {
+ return Hibernate.INTEGER;
+ }
+ };
+
+ private static SQLFunction classicAvg = new StandardSQLFunction("avg") {
+ public Type getReturnType(Type columnType, Mapping mapping) throws QueryException {
+ int[] sqlTypes;
+ try {
+ sqlTypes = columnType.sqlTypes( mapping );
+ }
+ catch ( MappingException me ) {
+ throw new QueryException( me );
+ }
+ if ( sqlTypes.length != 1 ) throw new QueryException( "multi-column type in avg()" );
+ int sqlType = sqlTypes[0];
+ if ( sqlType == Types.INTEGER || sqlType == Types.BIGINT || sqlType == Types.TINYINT ) {
+ return Hibernate.FLOAT;
+ }
+ else {
+ return columnType;
+ }
+ }
+ };
+
+ private static SQLFunction classicSum = new StandardSQLFunction("sum");
+
static {
STANDARD_AGGREGATE_FUNCTIONS.put( "count", new StandardSQLFunction("count") {
public Type getReturnType(Type columnType, Mapping mapping) {
@@ -104,9 +132,15 @@
}
if ( sqlTypes.length != 1 ) throw new QueryException( "multi-column type in sum()" );
int sqlType = sqlTypes[0];
- if ( sqlType == Types.FLOAT || sqlType == Types.DOUBLE ) {
+ if ( columnType == Hibernate.BIG_INTEGER ) {
+ return Hibernate.BIG_INTEGER;
+ }
+ if ( columnType == Hibernate.BIG_DECIMAL ) {
+ return Hibernate.BIG_DECIMAL;
+ }
+ if ( sqlType == Types.FLOAT || sqlType == Types.DOUBLE || sqlType == Types.NUMERIC ) {
return Hibernate.DOUBLE;
- } else if ( sqlType == Types.INTEGER || sqlType == Types.SMALLINT ) {
+ } else if ( sqlType == Types.INTEGER || sqlType == Types.SMALLINT || sqlType == Types.TINYINT ) {
return Hibernate.LONG;
}
else {
@@ -179,6 +213,16 @@
return getClass().getName();
}
+ /** Registers the pre 3.2 return types for count, avg and sum.
+ * Call this method in the constructor of your dialect subclass if you need
+ * the old types for aggregations.
+ **/
+ protected void registerClassicAggregationTypes() {
+ sqlFunctions.put( "sum", classicSum );
+ sqlFunctions.put( "avg", classicAvg );
+ sqlFunctions.put( "count", classicCount );
+ }
+
/**
* Characters used for quoting SQL identifiers
*/
Modified: trunk/Hibernate3/src/org/hibernate/engine/SessionFactoryImplementor.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/engine/SessionFactoryImplementor.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/src/org/hibernate/engine/SessionFactoryImplementor.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -22,6 +22,7 @@
import org.hibernate.cfg.Settings;
import org.hibernate.connection.ConnectionProvider;
import org.hibernate.dialect.Dialect;
+import org.hibernate.dialect.function.SQLFunctionRegistry;
import org.hibernate.exception.SQLExceptionConverter;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.stat.StatisticsImplementor;
@@ -164,4 +165,5 @@
public Set getCollectionRolesByEntityParticipant(String entityName);
public EntityNotFoundDelegate getEntityNotFoundDelegate();
+
}
Modified: trunk/Hibernate3/src/org/hibernate/persister/entity/AbstractEntityPersister.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/persister/entity/AbstractEntityPersister.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/src/org/hibernate/persister/entity/AbstractEntityPersister.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -2220,7 +2220,7 @@
final PreparedStatement update;
if ( callable ) {
CallableStatement callstatement = session.getBatcher().prepareCallableStatement( sql );
- callstatement.registerOutParameter( index++, Types.NUMERIC ); // TODO: should we require users to return number of update rows ?
+ callstatement.registerOutParameter( index++, Types.INTEGER ); // TODO: should we require users to return number of update rows ?
update = callstatement;
}
else if ( useBatch ) {
@@ -2267,11 +2267,13 @@
if ( useBatch ) {
session.getBatcher().addToBatch( 1 );
return true;
- }
- else {
+ } else if (!callable) {
return check( update.executeUpdate(), id, j );
+ } else {
+ update.executeUpdate();
+ CallableStatement cd = (CallableStatement)update;
+ return check( cd.getInt(1), id, j);
}
-
}
catch ( SQLException sqle ) {
if ( useBatch ) {
@@ -2336,7 +2338,7 @@
int index = 1;
if ( callable ) {
CallableStatement callstatement = session.getBatcher().prepareCallableStatement( sql );
- callstatement.registerOutParameter( index++, Types.NUMERIC ); // TODO: should we require users to return number of deleted rows ?
+ callstatement.registerOutParameter( index++, Types.INTEGER); // TODO: should we require users to return number of deleted rows ?
delete = callstatement;
}
else if ( useBatch ) {
@@ -2362,8 +2364,12 @@
if ( useBatch ) {
session.getBatcher().addToBatch( 1 );
}
- else {
+ else if(!callable) {
check( delete.executeUpdate(), id, j );
+ } else {
+ delete.executeUpdate();
+ CallableStatement cd = (CallableStatement)delete;
+ check( cd.getInt(1), id, j);
}
}
Modified: trunk/Hibernate3/test/org/hibernate/test/TestCase.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/TestCase.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/test/org/hibernate/test/TestCase.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -142,7 +142,7 @@
lastTestClass = getClass();
}
}
-
+
protected void runTest() throws Throwable {
final boolean stats = ( (SessionFactoryImplementor) sessions ).getStatistics().isStatisticsEnabled();
try {
@@ -160,6 +160,9 @@
}
else {
session=null;
+
+ //assertAllDataRemoved();
+
}
}
catch (Throwable e) {
@@ -181,6 +184,12 @@
}
}
+ private void assertAllDataRemoved() {
+ Session tmpSession = openSession();
+ assertEquals("Data is left in the database",0, tmpSession.createQuery( "from java.lang.Object" ).list().size());
+ tmpSession.close();
+ }
+
protected boolean dropAfterFailure() {
return true;
}
Modified: trunk/Hibernate3/test/org/hibernate/test/criteria/CriteriaQueryTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/criteria/CriteriaQueryTest.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/test/org/hibernate/test/criteria/CriteriaQueryTest.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -319,10 +319,10 @@
//s.flush();
- Integer count = (Integer) s.createCriteria(Enrolment.class)
+ Long count = (Long) s.createCriteria(Enrolment.class)
.setProjection( Projections.count("studentNumber").setDistinct() )
.uniqueResult();
- assertEquals(count, new Integer(2));
+ assertEquals(count, new Long(2));
Object object = s.createCriteria(Enrolment.class)
.setProjection( Projections.projectionList()
@@ -334,7 +334,7 @@
.uniqueResult();
Object[] result = (Object[])object;
- assertEquals(new Integer(2),result[0]);
+ assertEquals(new Long(2),result[0]);
assertEquals(new Long(667),result[1]);
assertEquals(new Long(101),result[2]);
assertEquals( 384.0, ( (Double) result[3] ).doubleValue(), 0.01 );
@@ -493,10 +493,10 @@
s.flush();
- Integer count = (Integer) s.createCriteria(Enrolment.class)
+ Long count = (Long) s.createCriteria(Enrolment.class)
.setProjection( Property.forName("studentNumber").count().setDistinct() )
.uniqueResult();
- assertEquals(count, new Integer(2));
+ assertEquals(count, new Long(2));
Object object = s.createCriteria(Enrolment.class)
.setProjection( Projections.projectionList()
@@ -508,7 +508,7 @@
.uniqueResult();
Object[] result = (Object[])object;
- assertEquals(new Integer(2),result[0]);
+ assertEquals(new Long(2),result[0]);
assertEquals(new Long(667),result[1]);
assertEquals(new Long(101),result[2]);
assertEquals(384.0, ( (Double) result[3] ).doubleValue(), 0.01);
Added: trunk/Hibernate3/test/org/hibernate/test/hql/CriteriaHQLAlignmentTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/hql/CriteriaHQLAlignmentTest.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/test/org/hibernate/test/hql/CriteriaHQLAlignmentTest.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -0,0 +1,181 @@
+//$Id: HQLTest.java 9873 2006-05-04 13:42:48Z max...@jb... $
+package org.hibernate.test.hql;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.*;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.hibernate.classic.Session;
+import org.hibernate.criterion.Projections;
+import org.hibernate.dialect.DB2Dialect;
+import org.hibernate.dialect.HSQLDialect;
+import org.hibernate.dialect.MySQLDialect;
+import org.hibernate.dialect.Oracle9Dialect;
+import org.hibernate.dialect.PostgreSQLDialect;
+import org.hibernate.dialect.function.SQLFunction;
+import org.hibernate.hql.ast.DetailedSemanticException;
+import org.hibernate.hql.ast.QuerySyntaxException;
+import org.hibernate.hql.ast.ASTQueryTranslatorFactory;
+import org.hibernate.hql.ast.QueryTranslatorImpl;
+import org.hibernate.hql.ast.tree.ConstructorNode;
+import org.hibernate.hql.ast.tree.DotNode;
+import org.hibernate.hql.ast.tree.IndexNode;
+import org.hibernate.hql.ast.tree.SelectClause;
+import org.hibernate.hql.QueryTranslatorFactory;
+import org.hibernate.hql.QueryTranslator;
+import org.hibernate.engine.SessionFactoryImplementor;
+import org.hibernate.engine.query.HQLQueryPlan;
+import org.hibernate.engine.query.ReturnMetadata;
+import org.hibernate.Criteria;
+import org.hibernate.Hibernate;
+
+import antlr.RecognitionException;
+
+/**
+ * Tests cases for ensuring alignment between HQL and Criteria behavior.
+ *
+ * @author Max Rydahl Andersen
+ */
+public class CriteriaHQLAlignmentTest extends QueryTranslatorTestCase {
+
+ public CriteriaHQLAlignmentTest(String x) {
+ super( x );
+ SelectClause.VERSION2_SQL = true;
+ }
+
+ protected boolean dropAfterFailure() {
+ return true;
+ }
+
+ protected boolean recreateSchema() {
+ return true; // needed for the Criteria return type test
+ }
+
+ public void testHQLAggregationReturnType() {
+ // EJB3: COUNT returns Long
+ QueryTranslatorImpl translator = createNewQueryTranslator( "select count(*) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
+
+ translator = createNewQueryTranslator( "select count(h.height) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
+
+ // MAX, MIN return the type of the state-field to which they are applied.
+ translator = createNewQueryTranslator( "select max(h.height) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
+
+ translator = createNewQueryTranslator( "select max(h.id) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
+
+ // AVG returns Double.
+ translator = createNewQueryTranslator( "select avg(h.height) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
+
+ translator = createNewQueryTranslator( "select avg(h.id) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
+
+ translator = createNewQueryTranslator( "select avg(h.bigIntegerValue) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
+
+ // SUM returns Long when applied to state-fields of integral types (other than BigInteger);
+ translator = createNewQueryTranslator( "select sum(h.id) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
+
+ translator = createNewQueryTranslator( "select sum(h.intValue) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
+
+ // SUM returns Double when applied to state-fields of floating point types;
+ translator = createNewQueryTranslator( "select sum(h.height) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
+
+ translator = createNewQueryTranslator( "select sum(h.floatValue) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
+
+ // SUM returns BigInteger when applied to state-fields of type BigInteger
+ translator = createNewQueryTranslator( "select sum(h.bigIntegerValue) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.BIG_INTEGER, translator.getReturnTypes()[0] );
+
+ // SUM and BigDecimal when applied to state-fields of type BigDecimal.
+ translator = createNewQueryTranslator( "select sum(h.bigDecimalValue) from Human h" );
+ assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
+ assertEquals( "incorrect return type", Hibernate.BIG_DECIMAL, translator.getReturnTypes()[0] );
+ }
+
+ public void testCriteriaAggregationReturnType() {
+ Session s = openSession();
+ Human human = new Human();
+ human.setBigIntegerValue( new BigInteger("42") );
+ human.setBigDecimalValue( new BigDecimal(45) );
+ s.save(human);
+
+ // EJB3: COUNT returns Long
+ Long longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.rowCount()).uniqueResult();
+ assertEquals(longValue, new Long(1));
+ longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.count("height")).uniqueResult();
+ assertEquals(longValue, new Long(1));
+
+ // MAX, MIN return the type of the state-field to which they are applied.
+ Double dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.max( "height" )).uniqueResult();
+ assertNotNull(dblValue);
+
+ longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.max( "id" )).uniqueResult();
+ assertNotNull(longValue);
+
+ // AVG returns Double.
+ dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "height" )).uniqueResult();
+ assertNotNull(dblValue);
+
+ dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "id" )).uniqueResult();
+ assertNotNull(dblValue);
+
+ dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "bigIntegerValue" )).uniqueResult();
+ assertNotNull(dblValue);
+
+ // SUM returns Long when applied to state-fields of integral types (other than BigInteger);
+ longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.sum( "id" )).uniqueResult();
+ assertNotNull(longValue);
+
+ longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.sum( "intValue" )).uniqueResult();
+ assertNotNull(longValue);
+
+ // SUM returns Double when applied to state-fields of floating point types;
+ dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.sum( "height" )).uniqueResult();
+ assertNotNull(dblValue);
+
+ dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.sum( "floatValue" )).uniqueResult();
+ assertNotNull(dblValue);
+
+ // SUM returns BigInteger when applied to state-fields of type BigInteger
+ BigInteger bigIValue = (BigInteger) s.createCriteria( Human.class ).setProjection( Projections.sum( "bigIntegerValue" )).uniqueResult();
+ assertNotNull(bigIValue);
+
+ // SUM and BigDecimal when applied to state-fields of type BigDecimal.
+ BigDecimal bigDValue = (BigDecimal) s.createCriteria( Human.class ).setProjection( Projections.sum( "bigDecimalValue" )).uniqueResult();
+ assertNotNull(bigDValue);
+
+ s.delete( human );
+ s.flush();
+ s.close();
+ }
+
+ public static Test suite() {
+ return new TestSuite( CriteriaHQLAlignmentTest.class );
+ }
+
+}
Modified: trunk/Hibernate3/test/org/hibernate/test/hql/HQLSuite.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/hql/HQLSuite.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/test/org/hibernate/test/hql/HQLSuite.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -22,6 +22,7 @@
suite.addTest( HqlParserTest.suite() );
suite.addTest( ScrollableCollectionFetchingTest.suite() );
suite.addTest( ClassicTranslatorTest.suite() );
+ suite.addTest( CriteriaHQLAlignmentTest.suite() );
return suite;
}
}
Modified: trunk/Hibernate3/test/org/hibernate/test/hql/HQLTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/hql/HQLTest.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/test/org/hibernate/test/hql/HQLTest.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -3,11 +3,15 @@
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.*;
import junit.framework.Test;
import junit.framework.TestSuite;
+import org.hibernate.classic.Session;
+import org.hibernate.criterion.Projections;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQLDialect;
@@ -27,6 +31,7 @@
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.query.HQLQueryPlan;
import org.hibernate.engine.query.ReturnMetadata;
+import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import antlr.RecognitionException;
@@ -803,66 +808,8 @@
assertTranslation( "select count(distinct an) from Animal an" );
assertTranslation( "select count(distinct an.id) from Animal an" );
assertTranslation( "select count(all an.id) from Animal an" );
- }
-
- public void testAggregationReturnType() {
- // EJB3: COUNT returns Long
- QueryTranslatorImpl translator = createNewQueryTranslator( "select count(*) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
+ }
- // MAX, MIN return the type of the state-field to which they are applied.
- translator = createNewQueryTranslator( "select max(h.height) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
-
- translator = createNewQueryTranslator( "select max(h.id) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
-
- // AVG returns Double.
- translator = createNewQueryTranslator( "select avg(h.height) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
-
- translator = createNewQueryTranslator( "select avg(h.id) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
-
- translator = createNewQueryTranslator( "select avg(h.bigIntegerValue) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
-
- // SUM returns Long when applied to state-fields of integral types (other than BigInteger);
- translator = createNewQueryTranslator( "select sum(h.id) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
-
- translator = createNewQueryTranslator( "select sum(h.intValue) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
-
- // SUM returns Double when applied to state-fields of floating point types;
- translator = createNewQueryTranslator( "select sum(h.height) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
-
- translator = createNewQueryTranslator( "select sum(h.floatValue) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
-
- // SUM returns BigInteger when applied to state-fields of type BigInteger
- translator = createNewQueryTranslator( "select sum(h.bigIntegerValue) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.BIG_INTEGER, translator.getReturnTypes()[0] );
-
- // SUM and BigDecimal when applied to state-fields of type BigDecimal.
- translator = createNewQueryTranslator( "select sum(h.bigDecimalValue) from Human h" );
- assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
- assertEquals( "incorrect return type", Hibernate.BIG_DECIMAL, translator.getReturnTypes()[0] );
-
- }
-
public void testSelectProperty() throws Exception {
assertTranslation( "select an.bodyWeight, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" );
}
@@ -1327,5 +1274,5 @@
public static Test suite() {
return new TestSuite( HQLTest.class );
}
-
+
}
Modified: trunk/Hibernate3/test/org/hibernate/test/immutable/ImmutableTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/immutable/ImmutableTest.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/test/org/hibernate/test/immutable/ImmutableTest.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -44,8 +44,8 @@
cv1 = (ContractVariation) c.getVariations().iterator().next();
assertEquals( cv1.getText(), "expensive" );
s.delete(c);
- assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );
- assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Integer(0) );
+ assertEquals( s.createCriteria(Contract.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) );
+ assertEquals( s.createCriteria(ContractVariation.class).setProjection( Projections.rowCount() ).uniqueResult(), new Long(0) );
t.commit();
s.close();
}
Modified: trunk/Hibernate3/test/org/hibernate/test/ops/MergeTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ops/MergeTest.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/test/org/hibernate/test/ops/MergeTest.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -262,7 +262,7 @@
s.createCriteria(NumberedNode.class)
.setProjection( Projections.rowCount() )
.uniqueResult(),
- new Integer(2)
+ new Long(2)
);
s.delete(root);
s.delete(mergedChild);
Modified: trunk/Hibernate3/test/org/hibernate/test/ops/SaveOrUpdateTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ops/SaveOrUpdateTest.java 2006-05-07 15:49:16 UTC (rev 9906)
+++ trunk/Hibernate3/test/org/hibernate/test/ops/SaveOrUpdateTest.java 2006-05-08 17:41:21 UTC (rev 9907)
@@ -242,7 +242,7 @@
s.createCriteria( NumberedNode.class )
.setProjection( Projections.rowCount() )
.uniqueResult(),
- new Integer( 2 )
+ new Long( 2 )
);
s.delete( root );
s.delete( child );
@@ -299,7 +299,7 @@
s.createCriteria( NumberedNode.class )
.setProjection( Projections.rowCount() )
.uniqueResult(),
- new Integer( 2 )
+ new Long( 2 )
);
s.delete( root );
s.delete( child );
@@ -353,7 +353,7 @@
s.createCriteria( Node.class )
.setProjection( Projections.rowCount() )
.uniqueResult(),
- new Integer( 2 )
+ new Long( 2 )
);
s.delete( root );
s.delete( child );
|