I've got a double which I'm trying to save to a postgres numeric column.
The value I'm trying to save is 151.33160591125488, and I've verified that this is in fact the argument being received by Hibernates internals pre-insert.
However the value in the database post insert is 151.331605911255, ie it's been rounded to 12dp.
I know my column is of the right scale, as it's an unrestricted numeric column and the following...
update tbl set col=151.33160591125488
...has the desired effect.
So the culprit has to be either Hibernate or the postgres-JDBC driver.
Ideas?
EDIT:
org.hibernate.dialect.PostgreSQLDialect:
registerColumnType( Types.DOUBLE, "float8" );
and
select cast (151.33160591125488 as float8);
=151.331605911255
therefore the default behavior for double is incorrect, as it doesn't always save the double supplied to it.
Does anyone know how to get hibernate to use the column type "numeric" in postgres?
i tried with precision=”30” scale=”20” which should have been plenty, same result. so bad advice.
– pstanton16 октября 2009, 01:55You haven’t really given enough details on the hibernate side. The mapping can specify precision and scale. for example:
just for your information, precision=”12” scale=”2” has no effect on inserts/updates, it is only used if hibernate also creates the database, so that it knows how to create the column.
– Mauli17 октября 2009, 07:59Thanks brian, i’ll try that however this would reduce the scalability of my app, all because hibernate is misbehaving. java double doesn’t restrict, postgres doesn’t restrict, why should hibernate?
– pstanton16 октября 2009, 01:49FYI, the “postgres” tag was recently replaced with “postgresql”. See http://meta.stackexchange.com/questions/25279/retag-request-postgres-postgresql for more info. I took the liberty of re-tagging this question.
– Matt Solnit16 октября 2009, 04:55