민뭉아치
민뭉아치
민뭉아치
전체 방문자
오늘
어제
  • 분류 전체보기 (62)
    • ㅁㅁㅇㅊ's Life (0)
      • Diary (1)
      • Certification (6)
      • English (1)
      • 독서 (1)
    • Algorithm (25)
    • Web (2)
    • Language (3)
      • JavaScript (1)
      • JAVA (2)
    • Spring boot (9)
    • Cloud (0)
    • DevOps (1)
    • Trouble Shooting (6)
    • 개발환경 (3)
    • 짤막IT지식 (4)

블로그 메뉴

  • 홈
  • 깃허브
  • 방명록

공지사항

인기 글

태그

  • 코딩테스트
  • 자바
  • 알고리즘
  • Java
  • 정보처리기사
  • Spring
  • 자격증
  • Algorithm
  • 백준
  • 백준알고리즘

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
민뭉아치

민뭉아치

Trouble Shooting

[Hibernate Error] TransientPropertyValueException : object references an unsaved transient instance

2022. 3. 21. 13:54

1. 문제 상황

프로젝트에서 새로운 User을 생성하기 위해 회원가입을 진행하던 도중 다음과 같은 에러가 발생했다.

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - 
save the transient instance before flushing : dev.sma.basic.jpa.entity.UserEntity.residence -> 
dev.sma.basic.jpa.entity.AreaEntity

 

2. 원인

영속성에 의해 발생한 오류라고 한다.

FK로 사용되는 칼럼에 데이터가 없는 상태에서 데이터를 넣으면 발생한다.

현재  나의 프로젝트 기준으로 UserEntity가 AreaEntity와 @ManyToOne 관계를 가지고 있다.

AreaEntity로 생성된  'area' 테이블에 데이터를 넣지않고 User 데이터를 넣으려고 하니 FK인 area_id가 없기 때문에 발생했다.

 

3. 해결

@ManytoOne, @OnetoMany 어노테이션에 cascade 옵션을 추가해주면 된다.

cascade = CascadeType.ALL

cascade 옵션을 설정해줌으로써 parent에 데이터를 저장할 때 Child 데이터를 저장하도록  hibernate에 알려준다.

나의 프로젝트에서는 cacscade 옵션에 의해 User 데이터를 저장할 때 Area 데아터를 저장하도록 해준다.

@ManyToOne(
	targetEntity = AreaEntity.class,
	fetch = FetchType.LAZY,
	cascade = CascadeType.ALL
)
@JoinColumn(name = "area_id")
private AreaEntity residence;

 

 

4. 참고 

  • https://stackoverflow.com/questions/2302802/how-to-fix-the-hibernate-object-references-an-unsaved-transient-instance-save
 

How to fix the Hibernate "object references an unsaved transient instance - save the transient instance before flushing" error

I receive following error when I save the object using Hibernate object references an unsaved transient instance - save the transient instance before flushing

stackoverflow.com

  • https://bcp0109.tistory.com/344
 

JPA 관련 Hibernate 에러: object references an unsaved transient instance - save the transient instance before flushing

에러 로그 TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing 원인 JPA 연관 관계 테스트 중에 발생했습니다. FK 로 사용되는..

bcp0109.tistory.com

 

반응형

'Trouble Shooting' 카테고리의 다른 글

리소스를 HTTPS 프로토콜 호출로 인한 ERR_SSL_PROTOCOL_ERROR 발생, Mixed Content  (0) 2024.04.17
Spring Boot Jpa IllegalArgumentException 에러  (2) 2022.05.18
AWS EC2 Gradle 배포 시 멈춤현상  (0) 2022.05.04
QLRM 네이티브 쿼리 DTO type 오류  (0) 2022.04.14
Spring boot와 h2 Database 연동 오류발생  (0) 2022.04.10
    'Trouble Shooting' 카테고리의 다른 글
    • Spring Boot Jpa IllegalArgumentException 에러
    • AWS EC2 Gradle 배포 시 멈춤현상
    • QLRM 네이티브 쿼리 DTO type 오류
    • Spring boot와 h2 Database 연동 오류발생
    민뭉아치
    민뭉아치
    Do Better Today Than You Were Yesterday

    티스토리툴바