Querydsl 에서 DB 데이터 값을 변환하여 select 하는 방법
DB의 값중 Float 값을 제대로 선택하려면
decimal 로 변환해서 찾는 방법이 있고,
SELECT * FROM your_table WHERE cast(my_float as decimal(5,1)) = 1.3;
leehblue.com/match-a-float-in-mysql/
How To Match A Float Column In MySQL - Lee Blue
The Float datatype in MySQL is inherently inaccurate. If you are planning to use a float datatype for a column in your database you should reconsider, especially if you are planning to use it to store money values. Here is an example of the problem. If you
leehblue.com
round() 함수를 이용해 찾는 방법이 있는데
github.com/querydsl/querydsl/issues/458
Round(x) Function · Issue #458 · querydsl/querydsl
I saw there is a round() function that return the closest int from the numeric variable. But there is no round(x) function that would return the numeric value with x decimal. For example: select ro...
github.com
MathExpressions (Querydsl 4.0.7 API)
Create a sign(num) expression Returns the positive (+1), zero (0), or negative (-1) sign of num.
www.querydsl.com
Querydsl 에서는 MathExpressions 의 round() 함수를 사용하여
다음과 같이 디비의 Float 값을 변환한 값으로 select 가능하다.
private BooleanBuilder numbersEq(Float[] numbers) {
BooleanBuilder builder = new BooleanBuilder();
for(Float num: numbers) {
builder.or(MathExpressions.round(product.serialNumber,2).eq(num));
}
return builder;
}
'JAVA > JPA & Querydsl' 카테고리의 다른 글
읽기 전용 Query, Transaction 성능 최적화 (0) | 2023.07.20 |
---|---|
상속 관계 Entity에서 자식 Entity 조회하기 with HibernateProxy (0) | 2023.07.19 |
SpringBoot3.x 버전에서 QueryDsl 사용 설정 시 이슈 정리 (0) | 2023.07.17 |