본문 바로가기

Programming!

Spring / QueryDSL int * int = long 의 경우

NumberPath<Integer> 컬럼에 대응하는 값들을 multiply 처리해 쓰다보면 실제 결과는 int 를 넘어가는 상황을 맞이하게 된다.

 

이때 ea (int) * salePrice(int)의 경우 as사용시 당연하지만 NumberPath가 Integer로 지정된다.

..
NumberPath<Integer> totalSalePrice = Expressions.numberPath(Integer.class, "totalSalePrice");
..
qOrderItem.ea.sum().multiply(qOrderItem.salePrice.min()).as(totalSalePrice))

근데 우린 장사가 잘되고 있으니 int의 범위를 훅 넘어버린다. 문제는 code에서 int로 했다고 해서 오류는 나지 않는다. 

(단지 결과만 음수처리되어 나온다...엌!)

 

int가 넘어가는 결과가 예상되는 경우는 꼭 다음과 같이 longValue()로 cast해주고 받는 값의 타입도 long 으로 지정해줘야 한다.

..
NumberPath<Long> totalSalePrice = Expressions.numberPath(Long.class, "totalSalePrice");
..
qOrderItem.ea.sum().multiply(qOrderItem.salePrice.min()).longValue().as(totalSalePrice))