Hibernate casts BigDecimal instead of Integer
I don't know why but hibernate tries to cast BigDecimal to one of my columns. This column is definied as Decimal(3,0) in my DB2 DB. So there can't be a BigDecimal.
hbm.xml
<composite-id name="Id" class="db2.P5Id">
....
<key-property name="land" type="int">
<column name="PFIELD1" precision="3"/>
</key-property>
....
</composite-id>
When I try to commit a query like
sf.createQuery("from P5 where type = 1 and land in (:cs)")
.setParameterList("cs", cses, Hibernate.INTEGER).list()
I get开发者_C百科 the ClassCastException. The provided Collection only has Integer values.
This looks like the "problem" discussed in this thread and the suggested fixworkaround is to subclass the DB2Dialect
:
import org.hibernate.dialect.DB2Dialect;
import java.sql.Types;
public class FixedDB2Dialect extends DB2Dialect {
public FixedDB2Dialect() {
super();
registerColumnType(Types.INTEGER, "decimal($p)");
registerColumnType(Types.NUMERIC, "decimal($p,$s)");
registerColumnType(Types.DECIMAL, "decimal($p,$s)");
}
}
There is maybe a Jira issue with an official patch but I couldn't find it.
I'm curious as to why your database was modeled to use a decimal type with 0 scale rather than an integer type? The behavior you are seeing (and the "issue" Pascal mentions) is because hibernate naturally assumes that a DECIMAL is a floating point type, and is baffled when you configure it to be an int.
Is it possible to change the datatype in db2 to SMALLINT? This would probably take up less space on the db (2 bytes rather than 3) and be more logically consistent.
If not, go with Pascal's solution.
精彩评论