JPA | not-null error

DoItDevΒ·2021λ…„ 5μ›” 27일
0
post-thumbnail

πŸ”₯ λ“€μ–΄κ°€κΈ° μ•žμ„œμ„œ

이번 ν¬μŠ€νŒ…μ˜ 경우 μ‚¬μ†Œν•œ μ‹€μˆ˜μ—μ„œ λ‚˜μ˜¨ .. 그런 μ—λŸ¬μ΄λ‹€.
JPA ν…ŒμŠ€νŠΈλ₯Ό ν•˜λ‹€κ°€ λ‚˜μ˜¨ μ—λŸ¬μΈλ° not-null proerty error 이닀.

Non-Null Property Error

not-null property references a null or transient value 
μŠ€ν¬λ¦°μƒ· 2021-05-27 μ˜€ν›„ 9 29 18

μžλ°” 객체에 데이터가 null 둜 λ“€μ–΄κ°ˆ λ•Œ λ‚˜λŠ” μ—λŸ¬μ΄λ‹€

Error Message

not-null property references a null or transient value : io.gonzo.jpa.app.domain.basic.Product.price; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : io.gonzo.jpa.app.domain.basic.Product.price
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : io.gonzo.jpa.app.domain.basic.Product.price; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : io.gonzo.jpa.app.domain.basic.Product.price

원인

λ”λ―Έν…Œμ΄ν„°λ₯Ό λ„£μ–΄μ£ΌλŠ” λ‘œμ§μ΄λ‹€. ν•˜μ§€λ§Œ 무언가 빠진듯 ν•˜μ˜€λ‹€..

ProductDTO dto = ProductDTO.builder()
          .type("ING")
          .name("test....")
          .productType(new ProductType("test..."))
          .img("project.jpg")
          .build();
repository.save(dto.toEntity());

ν•΄κ²°

dto μ—μ„œ 더미데이터λ₯Ό μ£Όμž… ν•΄μ€„λ•Œ null 둜 λ“€μ–΄κ°€κ³  μžˆμ—ˆλ‹€.

ProductDTO dto = ProductDTO.builder()
          .type("ING")
          .name("test....")
          .price(new BigDecimal(0))
          .productType(new ProductType("test..."))
          .img("project.jpg")
          .build();

repository.save(dto.toEntity());

μžλ°” κ°μ²΄μ—μ„œ nullable 속성을 false 둜 μ£Όμ—ˆλ‹€...
false λΌλŠ” 것은 null 을 ν—ˆμš©μ„ ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 것인데 ..
DTO에 ν…ŒμŠ€νŠΈλ‘œ 더미λ₯Ό μ£Όμž…ν• λ•Œ λͺ¨λ₯΄κ³  κΉŒλ¨Ήμ—ˆλ˜κ²ƒ κ°™λ‹€...

@Entity
@NoArgsConstructor
@Table(name = "PRODUCT")
public class Product extends DomainEntity {

    @Column(name = "TYPE" , nullable = false)
    private String type;

    @Column(name = "NAME" , nullable = false)
    private String name;

    @Column(name = "PRICE" , nullable = false)
    private BigDecimal price;

    @Column(name = "IMG")
    private String img;

    @ManyToOne
    private ProductType productType = null;

    @Builder
    public Product(String type, String name, BigDecimal price, String img, ProductType productType) {
        this.type = type;
        this.name = name;
        this.price = price;
        this.img = img;
        this.productType = productType;
    }

}
profile
Back-End Engineer

0개의 λŒ“κΈ€