【问题标题】:Does it make sense to specify precision and scale for BigDecimal when it has already set in DB (Oracle)?当 BigDecimal 已经在 DB (Oracle) 中设置时,为它指定精度和比例是否有意义?
【发布时间】:2020-01-31 15:14:05
【问题描述】:

当 BigDecimal 已经在 DB (Oracle) 中设置时,指定精度和小数位数是否有意义?

@Column(name = "TEST_COLUMN", precision = 10, scale = 4)

@Column(name = "TEST_COLUMN")

考虑到在 DB 中,该列已指定如下:

NUMBER(10,4)

Hibernate 读取后精度是否保持不变,还是需要再次指定?

【问题讨论】:

    标签: java oracle hibernate


    【解决方案1】:

    和 columnDefinition 一样,它只与 DDL 生成有关。

    来自 Javadoc:

    /**
     * (Optional) The precision for a decimal (exact numeric) 
     * column. (Applies only if a decimal column is used.)
     * Value must be set by developer if used when generating 
     * the DDL for the column.
     */
    int precision() default 0;
    

    【讨论】:

      【解决方案2】:

      不需要在休眠中指定精度/比例。一旦在数据库中定义,hibernate 将忽略此属性。

      如果你想让 hibernate 创建表,你只需要指定精度/比例。否则这些属性都是多余的

      【讨论】:

      • 您能否链接一个确认您声明的来源?
      • 个人经验,有多个应用程序从数据库中查询BigDecimals,而没有在映射中提供精度/比例。这也很容易测试(甚至在这里问之前你应该做的)
      • 这只是概念验证,暂时无法对其进行测试。
      猜你喜欢
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2018-03-30
      相关资源
      最近更新 更多