寫HQL的時候做運算發生傳回型態的錯誤

SELECT (round((PRD.Price-PRD.Cost)/PRD.Price, 3)*100) AS profit
FROM product PRD

HQL大概是上面這樣,然後回報下方問題。查了一下 應該是

「BigDecimal調用時,當傳入值的小數位數 大於設定的BigDecimal小數位數時,報出錯誤」

這樣的問題。

java.lang.ArithmeticException: Rounding necessary
	at java.math.BigDecimal.commonNeedIncrement(Unknown Source)
	at java.math.BigDecimal.needIncrement(Unknown Source)
	at java.math.BigDecimal.divideAndRound(Unknown Source)
	at java.math.BigDecimal.setScale(Unknown Source)
	at java.math.BigDecimal.toBigIntegerExact(Unknown Source)
	at org.hibernate.type.descriptor.java.BigIntegerTypeDescriptor.wrap(BigIntegerTypeDescriptor.java:105)
	at org.hibernate.type.descriptor.java.BigIntegerTypeDescriptor.wrap(BigIntegerTypeDescriptor.java:36)

如果是用SQLQuery的話

.addScalar("profit",  StandardBasicTypes.BIG_DECIMAL)

應該就可以了,

但是HQL不支援addScalar

所以在HQL再轉一次

CAST((round((PRD.prodPrice-PRD.prodCost)/PRD.prodPrice, 3)*100) AS big_decimal) AS profit

 

arrow
arrow

    咪卡恰比 發表在 痞客邦 留言(0) 人氣()