Hibernate - Custom Dialect的BigDecimal列映射 [英] Hibernate - BigDecimal column mapping for Custom Dialect
问题描述
我使用Hibernate作为我们的对象关系映射(Object-Relational Mapping),并为一个模糊的数据库提供了一个自定义的方言。
我从这个数据库中检索的实体有一个列如此:
@Column(name =GROSS_WEIGHT,precision = 9,scale = 3)
private BigDecimal总重量;
数据库将此列定义为数字,精度为9,比例为3。 p>
我可以看到Hibernate生成的用于检索数据的SQL,当我使用数据库查询工具执行相同的查询时,它会为GROSS_WEIGHT列返回'9.68'。但是,在由Hibernate检索的实体中,'grossWeight'字段包含值'10',其中比例
0和精度
of 2!
在我正在使用的自定义方言类中尝试过覆盖以下列类型:
registerColumnType(Types.DECIMAL,numeric($ p,$ s));
registerColumnType(Types.DOUBLE,numeric($ p,$ s));
registerColumnType(Types.NUMERIC,numeric($ p,$ s));
但它仍然会返回(整数)整数。
在应用程序的其他地方,我们使用Postgres方言从Postgres中检索对象。
任何想法我应该在方言中做我可以让Hibernate正确设置检索到的BigDecimal的精度/缩放比例吗?
解析方案好的,下载完Hibernate源代码后,使用NetBeans中的调试器进行调试,发现问题出在专有JDBC驱动程序的 ResultSet
子类中,而不是在Hibernate中。
getBigDecimal
方法始终返回值为0的值。
当我联系了JDBC驱动程序的开发人员,他发现了错误并修复了它。
I'm using Hibernate as our Object-Relational Mapping, with a custom dialect for an obscure database.
The Entity I'm retrieving from this database has a column thus:
@Column(name = "GROSS_WEIGHT", precision = 9, scale = 3)
private BigDecimal grossWeight;
The database has this column defined as numeric with a precision of 9 and a scale of 3.
I can see the SQL that Hibernate generates to retrieve the data, and when I perform the same Query using the database query tool it returns '9.68' for the GROSS_WEIGHT column.
However, in the Entity that is retrieved by Hibernate, the 'grossWeight' field contains the value '10', with a scale
of 0 and precision
of 2!
in the custom dialect class I'm using I've tried overriding the following Column Types:
registerColumnType( Types.DECIMAL, "numeric($p,$s)" );
registerColumnType( Types.DOUBLE, "numeric($p,$s)" );
registerColumnType( Types.NUMERIC, "numeric($p,$s)" );
but it stills returns just the (rounded) whole number.
This has worked elsewhere in the application where we retrieve objects from Postgres using the Postgres dialect.
Any idea what I should be doing in the dialect so I can get Hibernate to correctly set the precision/scale of the BigDecimal retrieved?
OK, after downloading the Hibernate sources, and stepping thru them with the debugger in NetBeans, I discovered that the problem lay in the proprietary JDBC driver's ResultSet
sub-class, not in Hibernate.
The getBigDecimal
method was always returning a value with a scale of 0.
When I contacted the developer of the JDBC driver, he spotted the bug and fixed it.
这篇关于Hibernate - Custom Dialect的BigDecimal列映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!