• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

OrderServiceTest에서 Rollback(false) annotation을 붙였을 때 에러가 나는 이유

24.03.15 16:03 작성 24.03.15 16:03 수정 조회수 112

0

[질문 내용]

  • 현재상황

OrderServiceTest에서 상품주문()을 테스트 중입니다.

테스트 중 에러가 나서 코드를 검토해보았는데,

@Rollback(false) Annotation이 있었습니다.

Rollback(false)자체가 테스트 결과에 직접적인 영향을 줄 수는 없다고 생각하여 무시했었습니다.

그러나 Rollback(false) 어노테이션을 지웠더니 테스트가 정상적으로 작동하였습니다.

  • 궁금한 점

구글링도 해보고, 강의도 다시 들어보았지만

Rollback(false) 어노테이션이 상품주문 테스트 성공/실패에 어떠한 영향을 미치는지 궁금합니다.

답변 2

·

답변을 작성해보세요.

0

이건화님의 프로필

이건화

질문자

2024.03.19

원인은 모르겠으나 해결하였습니다.

@OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name="delivery_id")
private Delivery delivery;

OrderEntity에서 delivery를 선언할 때 cascade = CascadeType.ALL이 빠져있었습니다.
CascadeType.ALL은 Order 엔티티가 저장될 때, delivery 엔티티도 자동 저장되는 설정으로 알고 있습니다.

그러나 cascade = CascadeType.ALL가 없는 것이 어떻게
Rollback(False)가 있을 때는 실패를 하고,
Rollback(False)가 없을 때는 성공을 하는 차이를 만들어내는지는 잘 모르겠습니다.

0

David님의 프로필

David

2024.03.16

안녕하세요. 이건화님, 공식 서포터즈 David입니다.

어떤 오류가 발생하였는지 오류와 관련된 정보를 추가로 제공해주실 수 있으실까요?

오류 메시지 전체를 올려주시면 답변에 큰 도움이 됩니다.

감사합니다.

이건화님의 프로필

이건화

질문자

2024.03.17

Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

2024-03-17T13:41:17.481+09:00 INFO 8456 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

2024-03-17T13:41:17.483+09:00 TRACE 8456 --- [ionShutdownHook] o.h.type.spi.TypeConfiguration$Scope : Handling #sessionFactoryClosed from [org.hibernate.internal.SessionFactoryImpl@531b1778] for TypeConfiguration

2024-03-17T13:41:17.483+09:00 DEBUG 8456 --- [ionShutdownHook] o.h.type.spi.TypeConfiguration$Scope : Un-scoping TypeConfiguration [org.hibernate.type.spi.TypeConfiguration$Scope@698b1c68] from SessionFactory [org.hibernate.internal.SessionFactoryImpl@531b1778]

2024-03-17T13:41:17.486+09:00 INFO 8456 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...

2024-03-17T13:41:17.496+09:00 INFO 8456 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.

> Task :test

OrderServiceTest > ��ǰ�ֹ�() FAILED

org.springframework.dao.InvalidDataAccessApiUsageException at EntityManagerFactoryUtils.java:368

Caused by: java.lang.IllegalStateException at ExceptionConverterImpl.java:152

Caused by: org.hibernate.TransientPropertyValueException at CascadingActions.java:372

1 test completed, 1 failed

> Task :test FAILED

FAILURE: Build failed with an exception.

* What went wrong:

Execution failed for task ':test'.

> There were failing tests. See the report at: file:///C:/Users/LG/Desktop/__/Workspace/ValuePoint/jpashop/jpashop/build/reports/tests/test/index.html

* Try:

> Run with --scan to get full insights.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.5/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 50s

5 actionable tasks: 2 executed, 3 up-to-date

글자수제한으로 인해 이만큼만 우선 올렸습니다.
혹시 부족하다면 추가로 더 올릴 수 있도록 하겠습니다.

이건화님의 프로필

이건화

질문자

2024.03.17

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : jpabook.jpashop.domain.Order.delivery -> jpabook.jpashop.domain.Delivery

Caused by: java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : jpabook.jpashop.domain.Order.delivery -> jpabook.jpashop.domain.Delivery

Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : jpabook.jpashop.domain.Order.delivery -> jpabook.jpashop.domain

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : jpabook.jpashop.domain.Order.delivery -> jpabook.jpashop.domain.Delivery

Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : jpabook.jpashop.domain.Order.delivery -> jpabook.jpashop.domain.Delivery

안녕하세요. 건화님

코드 확인이 필요합니다.

실제 동작하는 전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요

 

추가로 다음 내용도 코멘트 부탁드립니다.

1. 문제 영역을 실행할 수 있는 방법

2. 문제가 어떻게 나타나는지에 대한 상세한 설명


링크: 공식 서포터즈

링크: 자주하는 질문

감사합니다.

이건화님의 프로필

이건화

질문자

2024.03.17

https://drive.google.com/file/d/1kM9IvlapR9KVDt9tzk9GaiBoKuSAnr8w/view?usp=drive_link

1. 문제 영역을 실행할 수 있는 방법

test/java/jpabook.jpashop.Service의 OrderServiceTest에서 상품주문 test를 돌렸을 때의 문제입니다.

2. 문제가 어떻게 나타나는지에 대한 상세한 설명

Rollback(false)일 때는 Test가 실패하고,
Rollback(false)를 지우면 Test가 성공합니다.

이유가 궁금합니다.

안녕하세요. 이건화님

코드를 확인해보니 Order에서 Cascade 부분이 빠졌습니다.

다음 코드를 참고해서 추가해주세요.

public class Order {

    //@OneToOne //변경 전
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) //변경 후
    @JoinColumn(name = "delivery_id")
    private Delivery delivery;

추가로 다른 이슈이기는 한데, application.yml에서 ;MVCC=true 부분을 제거해주세요.

jdbc:h2:tcp://localhost/~/jpashop;MVCC=TRUE

감사합니다.