개발 일기

TIL - 스프링 프로젝트 에러 일기 본문

Java&Spring/Java

TIL - 스프링 프로젝트 에러 일기

flow123 2021. 11. 25. 12:58

(1) Null 에러 

 

Posting Class의 Posting 객체에 null 값이 들어감 (null 이면 안되는 property인데) 

 

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.example.thebestmeal_test.domain.Posting.postingFood; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.example.thebestmeal_test.domain.Posting.postingFood] with root cause

 

import com.example.thebestmeal_test.dto.PostingDto;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity

public class Posting extends Timestamped {

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Long postingId;

    // 음식 설명
    @Column(nullable = false)
    private String postingMemo;

    // Posting Food와 연결 //여기서 foodId 추가.
    @OneToOne
    @JoinColumn(nullable = false)
    private PostingFood postingFood;

    public Posting(PostingDto postingDto) {
        this.postingMemo = postingDto.getPostingMemo();

    }


}

 

원인: PostingFood 를 입력하지 않았는데 nullable = false이기 때문. postingFood값을 넣어주거나 nullable 을 참조화한다. 

이 에러를 마주하고 해결한 적이 있는데, Column을 조인했을 때, postingDto가 어떻게 이동하는지가 구체화가 안되서(PostingFood값도 같이 들어간다는 걸 생각을 못함) 원인파악을 못함. 

다음에 시도: null 을 검색해보고, nullable = false 로 지정된 column 이 있는지 확인한다. 

#Memo만 들어가게 하려면 Nullable=False를 지운다. 

#Memo와 Food 둘다 들어가게 하려면? input에서 넣었는데도 안된다. 

왜냐하면, Controller 는 DTO 형태로 처리되는데, DTO 에 PostingFood칼럼을 놓쳤다. 

@Getter
@Setter

//글 등록할 때 Repo에 저장될 것.. 추후 user 정보와 함께 저장.
public class PostingDto {

    private String postingMemo;

}

(2) 연관관계 매핑에서 JoinColumn은 테이블에 column 을 더해준다. 

@JoinColumn(name = "article_idx", nullable = false)

*article_idx 라는 이름의 칼럼을 생성해줌. 

아티클 클래스 가져와서 매핑해준다. 

 

 

현재 DB에서 포스트한 것을 보려면 (내장 h2-database) 

REPOSITORY에 저장까지 해야한다. 

아직 Post 만 구현했기 때문에 h2-console로 보려고 한다. 계속 whitelabel page가 떠서.. 기존 db를 지워보았다. 

그래도 안된다ㅜㅜ 나중에 파악해보자. 

 

 

#Post 요청을 보냈을 때, 

Response 는 Dto 객체가 받아준다. 

ArticleRequestDto 가 파라미터로 들어간 생성자도 클래스에서 만들어줘야 한다. (아니면 아래에서 new Article 할 떄, articleRequestDto 생성자를 생성할 수 없다고 나온다. 

 

ArticleService 클래스 

public Article setArticle(ArticleRequestDto articleRequestDto){
    Article article = new Article(articleRequestDto); //객체가 있어야 article 단위로 생성해줄 수 있따. 
    articleRepository.save(article);
    return article;
}

(8) 

Description:

Parameter 0 of constructor in com.example.thebestmeal_1126.controller.ChoicesController required a bean of type 'com.example.thebestmeal_1126.service.ChoiceService' that could not be found.


Action:
Consider defining a bean of type 'com.example.thebestmeal_1126.service.ChoiceService' in your configuration.

 

-> 알고보니 서비스 클래스에 Service 롬복 깜빡했다.. ChoiceService는 에러가 나는데 PostingService 는 멀쩡해서 보니 이런 차이가 있었다. 계속 비교해서보는 습관 가지자. 

 

#코드 점유 문제 

 

'Java&Spring > Java' 카테고리의 다른 글

classpath:/static/ 경로 설정  (0) 2021.11.27
인텔리제이 Application properties 파일  (0) 2021.11.26
Java의 생성자 작용 원리  (0) 2021.11.24
자바와 파이썬의 차이점  (0) 2021.10.28
반복문  (0) 2021.10.23
Comments