Light Blue Pointer
๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Developing/Trouble shooting

[Kotlin] Cannot set property id because no setter, no wither and it's not part of the persistence constructor

by Craft Fiend 2024. 4. 25.

๐Ÿšฉ์—๋Ÿฌ

2024-04-25T22:52:54.233+09:00 ERROR 21840 --- [core] [nio-8081-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalStateException: Cannot set property id because no setter, no wither and it's not part of the persistence constructor public kpring.chat.chatroom.model.ChatRoom()] with root cause java.lang.IllegalStateException: Cannot set property id because no setter, no wither and it's not part of the persistence constructor public kpring.chat.chatroom.model.ChatRoom()

 

์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค

 

โ›ณ ํ•ด๊ฒฐ

Model ์˜ id๋ฅผ val์—์„œ var๋กœ ๋ณ€๊ฒฝํ•ด์คŒ

 @Id
    val id: String? = null

 @Id
    var id: String? = null
@Document(collection = "chatrooms")
class ChatRoom(
    var members : MutableList<String>
){
    @Id
    var id: String? = null

    val createdAt: LocalDateTime = LocalDateTime.now()

    fun getUsers(): MutableList<String>{
        return members
    }

    fun addUsers(list : MutableList<String>){
        members.addAll(list)
    }

}

 

 

๐Ÿ”Ž์›์ธ

Kotlin์—์„œ val๋กœ ์„ ์–ธํ•  ์‹œ Java์˜ final๋กœ ๋ณ€ํ™˜๋˜๋Š”๋ฐ 

Spring Data 2.1๋ถ€ํ„ฐ immutable objects๋ฅผ ๋” ์ง€์›ํ•˜๊ณ  final์€ ๋” ์ด์ƒ ์ง€์›ํ•˜์ง€ ์•Š์•„ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค

 

์ฝ์–ด๋ณด๋ฉด ์ข‹์„ ๊ธ€
https://www.geeksforgeeks.org/final-vs-immutability-java/
https://discuss.kotlinlang.org/t/kotlin-val-var-vs-java-final-advantage/22908