diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java index d9de442c7..3e8761dd0 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java @@ -1499,7 +1499,7 @@ public class DBDictionary case JavaTypes.BIGDECIMAL: if (storeLargeNumbersAsStrings) return getPreferredType(Types.VARCHAR); - return getPreferredType(Types.DOUBLE); + return getPreferredType(Types.NUMERIC); case JavaTypes.NUMBER: if (storeLargeNumbersAsStrings) return getPreferredType(Types.VARCHAR); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/mapping/PrecisionTestEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/mapping/PrecisionTestEntity.java new file mode 100644 index 000000000..f26386724 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/mapping/PrecisionTestEntity.java @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.jdbc.mapping; + +import java.math.BigDecimal; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Version; + +@Entity +public class PrecisionTestEntity { + @Id + @GeneratedValue + private int id; + @Version + private int version; + + private double primDbl; + private Double dbl; + private BigDecimal bigDecimal; + + @Column(precision = 10) + private double primDblPrecis; + @Column(precision = 10) + private Double dblPrecis; + @Column(precision = 10) + private BigDecimal bigDecimalPrecis; + + @Column(scale = 10) + private double primDblScale; + @Column(scale = 10) + private Double dblScale; + @Column(scale = 10) + private BigDecimal bigDecimalScale; + + @Column(precision = 10, scale = 10) + private double primDblPrecisScale; + @Column(precision = 10, scale = 10) + private Double dblPrecisScale; + @Column(precision = 10, scale = 10) + private BigDecimal bigDecimalPrecisScale; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public double getPrimDbl() { + return primDbl; + } + + public void setPrimDbl(double primDbl) { + this.primDbl = primDbl; + } + + public Double getDbl() { + return dbl; + } + + public void setDbl(Double dbl) { + this.dbl = dbl; + } + + public BigDecimal getBigDecimal() { + return bigDecimal; + } + + public void setBigDecimal(BigDecimal bigDecimal) { + this.bigDecimal = bigDecimal; + } + + public double getPrimDblPrecis() { + return primDblPrecis; + } + + public void setPrimDblPrecis(double primDblPrecis) { + this.primDblPrecis = primDblPrecis; + } + + public Double getDblPrecis() { + return dblPrecis; + } + + public void setDblPrecis(Double dblPrecis) { + this.dblPrecis = dblPrecis; + } + + public BigDecimal getBigDecimalPrecis() { + return bigDecimalPrecis; + } + + public void setBigDecimalPrecis(BigDecimal bigDecimalPrecis) { + this.bigDecimalPrecis = bigDecimalPrecis; + } + + public double getPrimDblScale() { + return primDblScale; + } + + public void setPrimDblScale(double primDblScale) { + this.primDblScale = primDblScale; + } + + public Double getDblScale() { + return dblScale; + } + + public void setDblScale(Double dblScale) { + this.dblScale = dblScale; + } + + public BigDecimal getBigDecimalScale() { + return bigDecimalScale; + } + + public void setBigDecimalScale(BigDecimal bigDecimalScale) { + this.bigDecimalScale = bigDecimalScale; + } + + public double getPrimDblPrecisScale() { + return primDblPrecisScale; + } + + public void setPrimDblPrecisScale(double primDblPrecisScale) { + this.primDblPrecisScale = primDblPrecisScale; + } + + public Double getDblPrecisScale() { + return dblPrecisScale; + } + + public void setDblPrecisScale(Double dblPrecisScale) { + this.dblPrecisScale = dblPrecisScale; + } + + public BigDecimal getBigDecimalPrecisScale() { + return bigDecimalPrecisScale; + } + + public void setBigDecimalPrecisScale(BigDecimal bigDecimalPrecisScale) { + this.bigDecimalPrecisScale = bigDecimalPrecisScale; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/mapping/TestPrecisionMapping.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/mapping/TestPrecisionMapping.java new file mode 100644 index 000000000..64a9a5776 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/mapping/TestPrecisionMapping.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.persistence.jdbc.mapping; + +import java.sql.Types; + +import org.apache.openjpa.jdbc.meta.ClassMapping; +import org.apache.openjpa.jdbc.meta.FieldMapping; +import org.apache.openjpa.jdbc.schema.Column; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestPrecisionMapping extends SingleEMFTestCase { + + private static final String[] _DOUBLE_FIELDS = + { "primDbl", "dbl" }; + private static final String _BIG_DECIMAL_FIELD = "bigDecimal"; + + public void setUp() { + setUp(PrecisionTestEntity.class); + } + + public void testUnspecified() { + testDoubleMapping("", Types.DOUBLE,0,0); + testBigDecimalMapping("", Types.NUMERIC, 0, 0); + } + + public void testPrecisionOnly() { + // testDoubleMapping("Precis", Types.NUMERIC, 10, 0); + testBigDecimalMapping("Precis", Types.NUMERIC, 10, 0); + } + + public void testScaleOnly() { + // testDoubleMapping("Scale", Types.NUMERIC, 0 , 10); + testBigDecimalMapping("Scale", Types.NUMERIC, 0, 10); + } + + public void testPrecisionAndScale() { +// testDoubleMapping("PrecisScale", Types.NUMERIC,10,10); + testBigDecimalMapping("PrecisScale", Types.NUMERIC, 10, 10); + } + + private void testBigDecimalMapping(String fieldSuffix, int expectedType, + int expectedPrecision, int expectedScale) { + ClassMapping mapping = getMapping(PrecisionTestEntity.class); + FieldMapping fm = + mapping.getFieldMapping(_BIG_DECIMAL_FIELD + fieldSuffix); + + Column[] cols = fm.getColumns(); + assertEquals(1, cols.length); + assertEquals(expectedType, cols[0].getType()); + assertEquals(expectedPrecision, cols[0].getSize()); + assertEquals(expectedScale, cols[0].getDecimalDigits()); + } + + private void testDoubleMapping(String fieldSuffix, int expectedType, + int expectedPrecision, int expectedScale) { + ClassMapping mapping = getMapping(PrecisionTestEntity.class); + FieldMapping fm; + + for(String s : _DOUBLE_FIELDS) { + fm = mapping.getFieldMapping(s + fieldSuffix); + + Column[] cols = fm.getColumns(); + assertEquals(1, cols.length); + assertEquals(expectedType ,cols[0].getType()); + assertEquals(expectedPrecision, cols[0].getSize()); + assertEquals(expectedScale, cols[0].getDecimalDigits()); + } + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java index 299180eaa..168c6dc6f 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java @@ -68,9 +68,32 @@ public abstract class SingleEMFTestCase } } + /** + * Get the class mapping for a given entity + * + * @param name + * The Entity's name. + * + * @return If the entity is a known type the ClassMapping for the Entity + * will be returned. Otherwise null + */ protected ClassMapping getMapping(String name) { return (ClassMapping) emf.getConfiguration() .getMetaDataRepositoryInstance().getMetaData(name, getClass().getClassLoader(), true); } + + /** + * Get the class mapping for a given entity + * + * @param entityClass an entity class. + * + * @return If the entity is a known type the ClassMapping for the Entity + * will be returned. Otherwise null + */ + protected ClassMapping getMapping(Class entityClass) { + return (ClassMapping) emf.getConfiguration() + .getMetaDataRepositoryInstance().getMetaData(entityClass, + getClass().getClassLoader(), true); + } }