본문 바로가기

TroubleShooting

org.springframework.dao.DataIntegrityViolationException 해결

객체 클래스

package com.personal.board.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@ToString
@Table(indexes = {
        @Index(columnList = "title"),
        @Index(columnList = "hashtag"),
        @Index(columnList = "createdAt"),
        @Index(columnList = "createdBy"),
})
@EntityListeners(AuditingEntityListener.class)
@Entity
public class Article {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Setter @Column(nullable = false, length = 255) private String title;
    @Setter @Column(nullable = false, length = 10000) private String content;

    @Setter private String hashtag;

    //    양방향 바인딩
    @ToString.Exclude
    @OrderBy("id")
    @OneToMany(mappedBy = "article", cascade = CascadeType.ALL)
    private final Set<ArticleComment> articleComments = new LinkedHashSet<>();

    //  meta data
    @CreatedDate @Column(nullable = false) private LocalDateTime createdAt;
    @CreatedBy @Column(nullable = false, length = 100) private String createdBy;
    @LastModifiedDate @Column(nullable = false) private LocalDateTime modifiedAt;
    @LastModifiedBy @Column(nullable = false, length = 100) private String modifiedBy;

    protected Article() {
    }

    private Article(String title, String content, String hashtag) {
        this.title = title;
        this.content = content;
        this.hashtag = hashtag;
    }

    public static Article of(String title, String content, String hashtag) {
        return new Article(title, content, hashtag);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Article article)) return false;
        return id != null && id.equals(article.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

JpaRepositoryTest

	@DisplayName("insert test")
    @Test
    void givenTestData_whenInsert_thenWorksFine() {
        //  given
        long previousCount = articleRepository.count();

        //  when
        Article savedArticle = articleRepository.save(Article.of("new title", "new content", "#spring"));

        //  then
        assertThat(articleRepository.count()).isEqualTo(previousCount + 1);

    }

입력했을 때 리포지토리에 저장되는 것을 확인하는 테스트인데 실행하니

org.springframework.dao.DataIntegrityViolationException: 
not-null property references a null or transient value : 
com.personal.board.domain.Article.createdBy; 
nested exception is org.hibernate.PropertyValueException: 
not-null property references a null or transient value : 
com.personal.board.domain.Article.createdBy

org.springframework.dao.DataIntegrityViolationException: 
not-null property references a null or transient value : 
com.personal.board.domain.Article.createdBy; 
nested exception is org.hibernate.PropertyValueException: 
not-null property references a null or transient value : 
com.personal.board.domain.Article.createdBy

 

위와 같은 에러가 발생했다.

not null이어야 하는 곳에 null이 입력되어 발생하는 것 같아서

에러메시지에서 언급한 createdBy, modified 필드를 nullable = true로 수정했다.

 

결과는

 

테스트 성공

 

코드 자료는 깃헙에 있다.

https://github.com/hanseongseo/project-board.git