I have data class
data class Author(
val id: String,
val name: String,
val books: MutableList<Book> = mutableListOf()
) {}
And I wrote request using jooq
val resultSet = dsl.select(author.ID, author.NAME,
field(select(jsonArrayAgg(jsonObject(book.ID, book.PRICE)))
.from(books)
.where(book.AUTHOR_ID.eq(author.ID))
).`as`("books"))
.from(authors)
.fetchInto(Author::class.java)
But I get an exception if Author has no books. It is logical, because field books in data class Author is not nullable. I tried to add companion object to data class
companion object {
operator fun invoke(
id: String,
name: String,
books: MutableList<Book>? = null
) = Author(id, name, books ?: mutableListOf())
}
But it doesn't work, jooq still tries to use default constructor and I get an exception. Is there a way to fix it without making field books in data class Author nullable?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…