본문 바로가기
Back-End/Spring Boot

데이터베이스 decimal 타입 JPA 에서 표현하기

by 코젼 2024. 7. 9.
728x90
반응형

Double 등의 타입을 사용할 수 있지만, 부동 소수점 연산의 정밀도 문제로 인해 BigDecimal 이 더 안전되게 사용된다.

BigDecimal

@Entity
public class EntityClass {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(precision = 10, scale = 2)
    private BigDecimal decimalField;
}

 

@DecimalMin, @DecimalMax

0.00 ~ 99999.99 사이의 값을 가질 수 있도록 제한할 수 있다.

@Entity
public class EntityClass {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @DecimalMin(value = "0.00")
    @DecimalMax(value = "99999.99")
    @Column(precision = 10, scale = 2)
    private BigDecimal decimalField;
}
728x90
반응형

'Back-End > Spring Boot' 카테고리의 다른 글

@Transactional  (0) 2024.07.15
Spring Controller header 값 가져오기  (0) 2024.07.10
@UniqueConstraint 와 @Colume 의 unique 속성 차이  (0) 2024.07.09
JPA 적용 및 API 기능 추가하기  (0) 2024.05.29
JPA를 사용하는 이유  (0) 2024.04.16

댓글