【发布时间】:2021-05-18 15:03:51
【问题描述】:
在这种情况下,我需要在执行货币操作减法的字段中执行更新。该列被创建为DECIMAL(19,2) UNSIGNED。
首先我得到值:
static final String LOCK_AMOUNT_FOR_UPDATED = "select amount from Sample where uuid = :uuid for update ";
这是存储库:
@Query(value = LOCK_AMOUNT_FOR_UPDATED, nativeQuery = true)
Float getAmountForUpdate(@Param("uuid") String uuid);
这里是第一个问题,而不是返回 Float。 建议返回 BigDecimal?
然后我检查值并执行更新:
static final String DECREASE_AMOUNT = "update Sample set amount = amount - :amountToSubtract where uuid = :uuid ";
存储库:
@Modifying
@Query(value = DECREASE_AMOUNT, nativeQuery = true)
void decreaseAmount(@Param("amountToSubtract") Float amountNegotiated, @Param("uuid") String uuid);
当值为:1927369.70 时执行此操作。我得到:
Data truncation: Out of range value for column 'amount' at row 1
方法调用是这样的:
repository.decreaseNetAmount(amountNegotiated.floatValue(), uuid);
我注意到,当我选择值以及在 BigDecimal 中调用 .floatValue() 时,1927369.70 变成了 1927369.80。
将所有内容都用作 BigDecimal 的正确方法是什么,甚至是 nativeQuery 的参数?
【问题讨论】:
标签: jpa spring-data-jpa spring-data bigdecimal