Spring boot + Kotlin에서 JPA 관계설정 시 발생하는 문제

SeungHyuk Shin·2022년 1월 26일
0
post-thumbnail

Spring boot 토이 프로젝트를 kotlin으로 만들고 있는데 entity끼리 관계 설정을 하는 곳에서 문제가 생겼다. 코드와 나오는 오류는 다음과 같았다.

    @ManyToMany
    @JoinTable(
        name = "user_authority",
        joinColumns = [JoinColumn(name = "id", referencedColumnName = "id")],
        inverseJoinColumns = [JoinColumn(name = "authority_name", referencedColumnName = "authority_name")]
    )
    var authorities: Set<Authority>

org.hibernate.AnnotationException — Collection has neither generic type or OneToMany.targetEntity()

예외는 description 에 나와있는 거 처럼 hibernate OneToMany 에 쓰이는 Collection 은 generic type이면 안된다. hibernate는 실행시점에 어떤 table이 사용되는지 알수없기 때문에 concrete class만 관계설정 필드에 사용할수 있다.

set의 코틀린 인터페이스 정의를 보면 interface Set<out E> : Collection<E> 로 정의되어 있음을 알 수 있다. interface Set<out E>와 같은경우 자바로 컴파일 하게 되면 interface Set<? extends E>로 바뀌기 때문에 오류가 났다.

해결방법으로는 간단하게 MutableSet을 쓰면 해당 오류는 해결이 가능하다.MutableSet을 보게 되면 interface MutableSet<E> : Set<E>, MutableCollection<E>로 정의 되어있다.

오늘 Kotlin in action을 공부하면서 out 키워드에 대해서 공부했는데 공부하자마자 직접 접하게 되어서 신기하기도 했다.

0개의 댓글