0

I'm trying to create a database table automatically using Hibernate and I have a few columns that are of type double (in Java). These columns have different precisions associated with them. For example, you might want to see the speed of a vehicle like "56.23" km/h. However, right now I'm getting something like "56.232343536918" km/h.

I'm using the <property name="hibernate.hbm2ddl.auto">update</property> property in my hibernate.cfg.xml file in order to automatically create the table using the associated mapping file.

Here's how I declared one the columns:

<property name="accelerometer_y" type="java.lang.Double">
    <column name="ACCELEROMETER_Y" precision="4" scale="2"/>
</property>

So from what I read about this in Hibernate, this is the proper way of declaring a MySQL data type of DOUBLE(4,2). However, when Hibernate automatically generates the table, I get DOUBLE, not DOUBLE(4,2). I don't get the precision and scale numbers that I've specified.

Does anybody see what's wrong in here?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
Robert Ruxandrescu
  • 627
  • 2
  • 10
  • 22
  • have you tried with `@Column(precision=4, scale=2)` in your model class. – Rajith Pemabandu Apr 03 '17 at 15:51
  • I marked this as a duplicate because when you say `DOUBLE` in MySQL you always get 64-bit IEEE floating point. And, when you use floating point, you need to handle the rounding yourself. It's just the nature of the floating point beast. – O. Jones Apr 03 '17 at 15:58
  • So you mean I should manually alter the tables? Isn't Hibernate supposed to create them automatically with the precision and scales that I have specified in the mapping file? – Robert Ruxandrescu Apr 04 '17 at 09:39

0 Answers0