Kotlin으로 DTO를 만들 때는 데이터 전달에 특화된 data class를 사용합니다.
그럼 Entity는?
Entity와 data class
기본적으로 Entity Class는 Entity manager를 통해 상태가 변경되기도 하고, 자체적으로 도메인의 요구사항을 구현한 함수를 포함하기도 합니다.
데이터 전달의 용도 그 이상이기 때문에 data class보다는 일반 class가 적합합니다.
그리고 결정적으로..
Hibernate 공식 문서에서 이를 권장하지 않습니다.
A central feature of Hibernate is the ability to load lazily certain entity instance variables (attributes) via runtime proxies. This feature depends upon the entity class being non-final or else implementing an interface that declares all the attribute getters/setters. You can still persist final classes that do not implement such an interface with Hibernate, but you will not be able to use proxies for fetching lazy associations, therefore limiting your options for performance tuning. For the very same reason, you should also avoid declaring persistent attribute getters and setters as final.
Hibernate의 핵심 기능인 지연 로딩을 final 클래스에서는 사용할 수 없습니다.
지연 로딩은 final이 아닌 클래스 또는 모든 attribute의 게터/세터를 선언한 인터페이스를 구현한 클래스에서만 동작합니다.
그러므로 지연 로딩을 이용하려면, 클래스가 기본적으로 final인 Kotlin에서는 open class로 정의해야 합니다.
하지만 data class는 open class가 될 수 없으므로... 절대 지연 로딩을 이용할 수 없습니다.
결론
data class에 @Entity 어노테이션을 달고, 속성에 fetch = FetchType.LAZY 옵션을 주어도 에러는 없습니다.
다만 해당 속성이 즉시 로딩될 뿐입니다...ㅎㅎ
'Web' 카테고리의 다른 글
| [Java] UTC와 ISO 8601, OffsetDateTime (3) | 2023.12.28 |
|---|---|
| [Spring] Swagger 적용하기 (2) | 2023.12.27 |
| [Spring] Parameter 0 of constructor in A required a bean of type B that could not be found (3) | 2023.12.21 |
| REST의 근원을 찾아서... (2) | 2023.12.20 |
| Spring을 사용하기 전에 알아야 할 개념들 (2) | 2023.12.19 |