[Kotlin, Spring] org.hibernate.HibernateException: Getter methods of lazy classes cannot be final:

19·2024년 4월 27일
0

에러 모음

목록 보기
22/24

배경

  1. Kotlin + Spring + JPA를 사용중
  2. 모든 엔티티에서 공통으로 사용할 시간 필드들을 따로 빼둔 추상 클래스를 정의하고 상속시킴
@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class BaseEntity {
    @CreatedDate
    lateinit var createdDateTime: LocalDateTime

    @LastModifiedDate
    lateinit var modifiedDateTime: LocalDateTime
}

org.hibernate.HibernateException: Getter methods of lazy classes cannot be final: 예외 발생

  • lazy 클래스의 getter가 final이면 안된다
    -> 코틀린에서는 상속을 해주려면 'open' 키워드를 넣어줘야 한다

내가 한 방법

@AllOpen 어노테이션을 정의하고 해당 어노테이션이 붙은 클래스들은 'open'시키도록 구현

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class AllOpen

build.gradle)

plugins {
    id "org.jetbrains.kotlin.plugin.allopen" version "1.8.22"
}

allOpen {
    annotation("com.testcafekiosk.spring.config.annotation.AllOpen")
}


@AllOpen 적용)

@AllOpen
@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class BaseEntity {
    @CreatedDate
    lateinit var createdDateTime: LocalDateTime

    @LastModifiedDate
    lateinit var modifiedDateTime: LocalDateTime
}

참고

https://kotlinlang.org/docs/all-open-plugin.html#gradle
https://v3.leedo.me/devs/81

profile
하나씩 차근차근

0개의 댓글