context.select(
jsonObject(
key("id").value(CHAPTER.ID),
key("name").value(CHAPTER.NAME),
key("createdAt").value(CHAPTER.CREATED_AT),
key("topics").value(
jsonArrayAgg(
jsonObject(
key("id").value(TOPIC.ID),
key("name").value(TOPIC.NAME),
key("createdAt").value(TOPIC.CREATED_AT),
)
)
)
)
)
.from(CHAPTER)
.join(TOPIC)
.on(CHAPTER.ID.eq(TOPIC.CHAPTER_ID))
.fetch().into(Chapter.class);
When I implement the above jooq statement it working file . In the select statement I have to specify all the columns of the table if no they are not being loaded in the result. Is there any simple way to select all the columns in the table instead of writing each and every "key().value()" pair for every column while using the join in the jooq?
CodePudding user response:
You can use the jsonObject(Field...)
overload, and use that as such:
jsonObject(MY_TABLE.fields())
It will produce the field name as a JSON object key, which doesn't seem to be exactly what you want, you seem to want to use a camel case notation. You can still do that dynamically, using this approach:
jsonObject(Arrays
.stream(MY_TABLE.fields())
.map(f -> key(toCamelCase(f.getName())).value(f))
.toList()
)
All you have to do is implement the toCamelCase()
auxiliary method, or use one from the java.beans
module, or from Spring, etc.