diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java index fea16552b57a..24050076f42e 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java @@ -15,6 +15,7 @@ import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.function.AvgWithArgumentCastFunction; import org.hibernate.dialect.function.NoArgSQLFunction; +import org.hibernate.dialect.function.RoundFunction; import org.hibernate.dialect.function.StandardSQLFunction; import org.hibernate.dialect.function.VarArgsSQLFunction; import org.hibernate.dialect.hint.IndexQueryHintHandler; @@ -163,7 +164,7 @@ public H2Dialect() { registerFunction( "power", new StandardSQLFunction( "power", StandardBasicTypes.DOUBLE ) ); registerFunction( "radians", new StandardSQLFunction( "radians", StandardBasicTypes.DOUBLE ) ); registerFunction( "rand", new NoArgSQLFunction( "rand", StandardBasicTypes.DOUBLE ) ); - registerFunction( "round", new StandardSQLFunction( "round", StandardBasicTypes.DOUBLE ) ); + registerFunction( "round", new RoundFunction( "round" ) ); registerFunction( "roundmagic", new StandardSQLFunction( "roundmagic", StandardBasicTypes.DOUBLE ) ); registerFunction( "sign", new StandardSQLFunction( "sign", StandardBasicTypes.INTEGER ) ); registerFunction( "sin", new StandardSQLFunction( "sin", StandardBasicTypes.DOUBLE ) ); @@ -425,12 +426,12 @@ public boolean doesReadCommittedCauseWritersToBlockReaders() { // see http://groups.google.com/group/h2-database/browse_thread/thread/562d8a49e2dabe99?hl=en return true; } - + @Override public boolean supportsTuplesInSubqueries() { return false; } - + @Override public boolean dropConstraints() { // We don't need to drop constraints before dropping tables, that just leads to error diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/RoundFunction.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/RoundFunction.java new file mode 100644 index 000000000000..3b7fd9890c85 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/RoundFunction.java @@ -0,0 +1,38 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ + +package org.hibernate.dialect.function; + +import org.hibernate.engine.spi.Mapping; +import org.hibernate.type.StandardBasicTypes; +import org.hibernate.type.Type; + +/** + * When there is need to use round function with other function (i.e.sum()), + * its return type becomes Double by default if it is registered as a StandardSQLFunction. + * This function provides the feasibility to return BigDecimal as return type if argument is + * BigDecimal + * + * @author Mayur Bhindi + */ +public class RoundFunction extends StandardSQLFunction { + + /** + * Constructs RoundFunction + * + * @param name + */ + public RoundFunction(String name) { + super( name ); + } + + @Override + public Type getReturnType(Type firstArgumentType, Mapping mapping) { + return firstArgumentType == StandardBasicTypes.BIG_DECIMAL ? firstArgumentType : StandardBasicTypes.DOUBLE; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/dialect/H2DialectTest.java b/hibernate-core/src/test/java/org/hibernate/dialect/H2DialectTest.java new file mode 100644 index 000000000000..56e801c7c586 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/dialect/H2DialectTest.java @@ -0,0 +1,48 @@ +package org.hibernate.dialect; + +import java.util.Map; + +import org.hibernate.dialect.function.SQLFunction; +import org.hibernate.type.StandardBasicTypes; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Unit test of the behavior of the H2Dialect + * + * @author Mayur Bhindi + */ +public class H2DialectTest { + + private H2Dialect dialect; + + @Before + public void setup() { + dialect = new H2Dialect(); + } + + @After + public void tearDown() { + dialect = null; + } + + @Test + @TestForIssue(jiraKey = "HHH-15228") + public void testRoundFunction() { + Map functions = dialect.getFunctions(); + SQLFunction sqlFunction = functions.get( "round" ); + assertEquals( StandardBasicTypes.DOUBLE, sqlFunction.getReturnType( null, null ) ); + assertEquals( StandardBasicTypes.DOUBLE, sqlFunction.getReturnType( StandardBasicTypes.INTEGER, null ) ); + assertEquals( + StandardBasicTypes.BIG_DECIMAL, + sqlFunction.getReturnType( StandardBasicTypes.BIG_DECIMAL, null ) + ); + + } +}