[英]Fetching One to Many associations with JooQ
I'm trying to deserialise a one to many association with JooQ (without code generation) as per this post .我正在尝试按照这篇文章反序列化与 JooQ(没有代码生成)的一对多关联。
Here are my target classes.这是我的目标课程。
public class Author {
private Long id;
private String name;
private List<Book> books;
}
public class Book {
private String name;
}
My JooQ query is as follows:我的 JooQ 查询如下:
dslContext
.select(table("authors").asterisk(),
field(
select(jsonArrayAgg(
jsonObject(
jsonEntry("name", field("books.name")))))
.from(table("books"))
.join(table("authors"))
.on(field("books.author_id").eq(field("authors.id")))
.where(field("emails.collection_case_id")
.eq(field("collection_cases.id")))
).as("books"))
.from(table("authors"))
.where(trueCondition())
.fetchInto(Author.class);
The jsonObject()
method does not work as expected for me. jsonObject()
方法对我来说没有按预期工作。 The generated SQL statement looks something like this:生成的 SQL 语句如下所示:
select authors.*, (select json_agg(json_build_object(?, books.name)) from books join authors ...
The translated postgres query has not properly replaced the key attribute of json_build_object
and this results in SQL exception.翻译后的 postgres 查询没有正确替换
json_build_object
的 key 属性,这会导致 SQL 异常。
PS: I'm using JooQ 3.14.0 with postgres 11.5 PS:我使用 JooQ 3.14.0 和 postgres 11.5
虽然我无法使用各种 PostgreSQL 服务器和 JDBC 驱动程序版本在我这边重现这个问题,但这里的简单解决方法是使用DSL.inline(String)
来防止 jOOQ 为json_build_object()
函数参数生成绑定变量:
jsonEntry(inline("name"), field("books.name"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.