I use kotlin exposed sql in my project I created kotlin object for my table also I created DAO as is in example all working as should, but when I want return List of my DAO objects I get:
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.domcek.dto.Page["columns"]->java.util.ArrayList[0]->org.jetbrains.exposed.sql.Column["table"]->org.domcek.dto.Page["columns"]->java.util.ArrayList[0]->org.jetbrains.exposed.sql.Column["table"]->org.domcek.dto.Page["columns"]->java.util.ArrayList[0]->org.jetbrains.exposed.sql.Column["table"]->org.domcek.dto.Page["columns"]->java.util.ArrayList[0]->org.jetbrains.exposed.sql.Column["table"]->org.domcek.dto.Page["columns"]->java.util.ArrayList[0]->org.jetbrains.exposed.sql.Column["table"]->org.domcek.dto.Page["columns"]->java.util.ArrayList[0]->org.jetbrains.exposed.sql.Column["table"]->org.domcek.dto.Page["columns"]....
I use jackson in my KTOR aplication
myDao:
class PageDao(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<PageDao>(Page)
var title by Page.title
var body by Page.body
var parentId by Page.parentId
var active by Page.active
var order by Page.order
var slug by Page.slug
var children: List<PageDao>? = null
fun isMyParent(pageId: Int): Boolean {
return pageId == this.parentId
}
}
myHandler:
suspend fun findAll(call: ApplicationCall) {
val pages = pageRepository.getMainPages()
call.respond(pages)
}
Repository:
fun getMainPages(): List<PageDao> {
return transaction {
addLogger(StdOutSqlLogger)
return@transaction PageDao.find {
Page.active.eq(true)
Page.parentId.isNull()
}.toList()
}
}
The simplest way (IMHO) is using a DTO (data transfer object). For example (User - DAO class):
class UserJson(val name:String=""){ constructor(user: User):this(user.name) }
respond:
get(""){
val users = ArrayList<UserJson>()
transaction {
for (user in User.all())
users.add(UserJson(user))
}
call.respond(users)
}
A using JacksonView or field annotation is looked like more complicated method.