[Kotlin, Spring] Caused by: org.hibernate.AnnotationException: Collection ... is declared with a raw type and has an explicit 'targetEntity'

19·2024년 4월 27일
0

에러 모음

목록 보기
23/24

배경

  1. kotlin + Spring + Jpa 사용중
  2. Order(1) : OrderProduct(N) 양방향 관계로, Order에 있는 OrderProduct의 list값 (orderProducts)을 세팅중에 예외 발생
    Caused by: org.hibernate.AnnotationException: Collection 'com.testcafekiosk.spring.domain.order.Order.orderProducts' is declared with a raw type and has an explicit 'targetEntity'

내가 한 방법

결론부터 말하면 List<OrderProduct>로 선언한 게 문제였다

@Entity
@Table(name = "orders")
class Order(
  @OneToMany(mappedBy = "order", cascade = [CascadeType.ALL])
  var orderProducts: List<OrderProduct> = mutableListOf()
  ...
) {
  ...
}

코틀린에서는 List Interface의 구현이 List<out E>로 되어 있고, 이 때문에 List<? extends OrderProduct>로 되어버린 것


MutableList Interface의 구현은 MutableList<E>로 되어 있어 MutableList로 변경해서 해결

@Entity
@Table(name = "orders")
class Order(
  @OneToMany(mappedBy = "order", cascade = [CascadeType.ALL])
  var orderProducts: MutableList<OrderProduct> = mutableListOf()
  ...
) {
  ...
}

참고

https://medium.com/@SungMinLee/spring-boot-kotlin-%EC%97%90%EC%84%9C-onetomany-relationship-%EC%97%90%EC%84%9C-%EB%B0%9C%EC%83%9D%ED%95%98%EB%8A%94-%EB%AC%B8%EC%A0%9C-4d84e4875986

profile
하나씩 차근차근

0개의 댓글