What are the best workarounds for known problems with Hibernate's schema validation of floating point columns when using Oracle 10g?

前端 未结 4 1909
孤独总比滥情好
孤独总比滥情好 2021-01-07 20:10

I have several Java classes with double fields that I am persisting via Hibernate. For example, I have

@Entity
public class Node ...

  private double value         


        
相关标签:
4条回答
  • 2021-01-07 20:33

    There was a similar problem HHH-1598 with HSQL mappings of boolean fields, and a discussion of it here.

    The solution I chose to use was in the discussion referenced above, with an extension of HSQLDialect.

    I saw no problems with this, though I only use HSQL in tests.

    It certainly doesn't interfere with any other DB.

    0 讨论(0)
  • 2021-01-07 20:40

    just adding (columnDefinition = "NUMBER(9,2)") works!

    @Column(name = "CREDIT_AMOUNT", columnDefinition = "NUMBER(9,2)")
    @Basic
    private double creditAmount;
    
    0 讨论(0)
  • 2021-01-07 20:47

    This is a known limitation of the schema validator, check HHH-2315. So you have three options here (actually four but I guess that deactivating validation is not wanted). Either:

    • Use a float instead of a double at the Java level - this might not be an option though.

    • Patch org.hibernate.mapping.Table.validateColumns(Dialect dialect, Mapping mapping, TableMetadata tableInfo) to add a special condition for this particular case - this isn't really a light option.

    • Extends the org.hibernate.dialect.Oracle10gDialect to make it use float for the SQL type DOUBLE

      public class MyOracle10gDialect extends Oracle10gDialect {
          public MyOracle10gDialect() {
              super();
          }
          protected void registerNumericTypeMappings() {
              super.registerNumericTypeMappings();
              registerColumnType( Types.DOUBLE, "float" );
          }
      }
      

    The later option seems safe but will require some testing to see if it doesn't introduce any regression. I didn't look at Oracle's JDBC driver code, so I can't say how float and double precision differ at the driver level.

    0 讨论(0)
  • 2021-01-07 20:54

    Use 'scale' attribute on your member.

    0 讨论(0)
提交回复
热议问题