HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
♥️
2기 최종 프로젝트 팀별 공간
/
🌳
[팀 05] Forest
/
🪐
BE WorkSpace
/
RidingPost Entity update 시 발생한 문제

RidingPost Entity update 시 발생한 문제

분류
JPA
담당자

문제 발생

[feat] 라이딩 모집 글 수정 by kkj0419 · Pull Request #90 · prgrms-web-devcourse/Team-Forest-RG-BE
CreateRequest -> SaveRequest 로 네이밍 바꾸었습니다(생성, 수정 시 사용하도록) 수정 시에 sub-section 부분 처리를 전체 삭제 후 새로 삽입 하는 방식으로 처리했습니다 sub-section의 모든 필드가 수정 가능하기 때문에, 수정 시점에 들어온 subsection이 기존에 있던 것인지 알 수 없다고 판단했기 때...
[feat] 라이딩 모집 글 수정 by kkj0419 · Pull Request #90 · prgrms-web-devcourse/Team-Forest-RG-BE
https://github.com/prgrms-web-devcourse/Team-Forest-RG-BE/pull/90/files#r942165970
[feat] 라이딩 모집 글 수정 by kkj0419 · Pull Request #90 · prgrms-web-devcourse/Team-Forest-RG-BE
 
 
RidingPost - RidingConditionBicycle 에는 1:N 관계가 설정되어 있는 상태
RidingPost entity를 저장하기 이전에, RidingConditionBicycle을 저장해야 한다는 예외 발생
 
 
매핑된 상태
부모 객체(RidingPost) 부분에 영속성 전이를 설정하였음에도 위의 에러가 출력되었음
 
 
양쪽에 영속성 전이 설정하여 위의 예외 해결
 
 
 
 
어떤 영속 객체에서 사용하고 있는 객체를 다른 영속 객체에 주입시켜 사용하여(공유) 예외 발생
 
ridingpost를 update하는 방식을 다음과 같이 사용하고 있었는데,
이 과정에서 컬렉션의 값들을 공유하게 된 것으로 추정……
 
 
 
위와 같이 컬렉션의 데이터들을 clear한 후, 다시 add하는 방식으로 변경하여 해결
 
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.prgrms.rg.domain.ridingpost.model.RidingConditionBicycle.post -> com.prgrms.rg.domain.ridingpost.model.RidingPost java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.prgrms.rg.domain.ridingpost.model.RidingConditionBicycle.post -> com.prgrms.rg.domain.ridingpost.model.RidingPost at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:151) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1411) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1394) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568)
//RidingPost 부분(cascade 지정) ... @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private Set<RidingConditionBicycle> bicycleList = new HashSet<>(); //RidingConditionBicycle 부분(cascade 지정하지 않음) @ManyToOne(optional = false, fetch = FetchType.LAZY) private RidingPost post;
//RidingConditionBicycle @ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) @JoinColumn(name = "post_id") private RidingPost post;
org.hibernate.HibernateException: Found shared references to a collection: com.prgrms.rg.domain.ridingpost.model.RidingPost.ridingConditionSection.bicycleList javax.persistence.PersistenceException: org.hibernate.HibernateException: Found shared references to a collection: com.prgrms.rg.domain.ridingpost.model.RidingPost.ridingConditionSection.bicycleList at app//org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154) at app//org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) at app//org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
public void changePost(RidingPost newPost) { this.ridingMainSection = newPost.getRidingMainSection(); this.ridingParticipantSection = newPost.getRidingParticipantSection(); this.ridingConditionSection = newPost.getRidingConditionSection(); }
public void changePost(RidingPost newPost) { subSectionList.clear(); ridingMainSection.update(newPost.getRidingMainSection()); ridingParticipantSection.update(newPost.getRidingParticipantSection()); ridingConditionSection.update(newPost.getRidingConditionSection()); }
public void update(RidingConditionSection section) { setLevel(level); bicycleList.retainAll(section.getBicycleList()); bicycleList.addAll(section.getBicycleList()); }