SH380 Logo
2025-08-19

Test Data 삽입 시 JPA 컬렉션 초기화 문제

#SpringbootJPA#server#Troubleshooting

Caused by: org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.project.moyora.app.domain.Board.applications

문제 원인

해결 방법

// Board 객체 생성 (confirmed=true)
Board board = Board.builder()
        .id(i)
        .writer(writer) // 공통 writer
        .title(title)
        .content(content)
        .meetDetail(meetDetail)
        .tags(List.of(InterestTag.SPORTS)) // 예시는 SPORTS로 고정
        .genderType(GenderType.FEMALE)
        .minAge(18 + random.nextInt(10))
        .maxAge(25 + random.nextInt(10))
        .howMany(2 + random.nextInt(5))
        .participation(0)
        .meetType(randomMeetType())
        .startDate(LocalDate.now())
        .endDate(LocalDate.now().plusDays(random.nextInt(30)))
        .confirmed(true) // 확정 모임
        .createdTime(LocalDateTime.now())
        .applications(List.of()) // <- 빈 리스트로 초기화
        .build();

*JPA orphanRemoval 주의

1. 의미

org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance

2. 흔한 오류

3. 안전 사용법

board.getApplications().clear(); 
board.getApplications().add(newApp);

핵심

목록으로 돌아가기