Back-end/Spring

JPA, Hibernate 에러 -> org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing :

yujin0517 2023. 9. 25. 00:59

에러

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing :

 

원인

-> FK로 사용되는 컬럼 값이 없는 상태에서 데이터를 추가하려다 나타난 에러이다. 

예를 들어, Character 컬럼이 PK, Exp 컬럼이 FK일 때, PK 데이터를 넣지 않고 FK 데이터를 추가하려고 할 때 발생하는 오류이다. 

 

해결 방법

연관관계 매핑 어노테이션(@OneToOne, @OneToMany, @ManyToOne)을 사용할 때, cascade 옵션을 같이 설정해준다. 

cascade는 영속성 전이라고 하는데, 특정 엔티티를 영속화할 때 연관된 엔티티도 함계 영속화 시킨다. 

 @OneToOne(cascade = CascadeType.ALL)

위와 같이 설정하면 Exp 데이터를 저장하기 전에 Character 데이터부터 저장하여 에러를 막을 수 있다. 

 

 

 

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

ResponseEntity 클래스  (0) 2023.10.17
어노테이션, HandlerMethodArgumentResolver, DTO  (1) 2023.10.10
Maven 이란?  (0) 2023.07.31
어노테이션(Annotation) 정리  (0) 2023.04.20
[Spring] 오류 목록  (0) 2023.04.18