简体   繁体   中英

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 .

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:

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. The generated SQL statement looks something like this:

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.

PS: I'm using JooQ 3.14.0 with postgres 11.5

虽然我无法使用各种 PostgreSQL 服务器和 JDBC 驱动程序版本在我这边重现这个问题,但这里的简单解决方法是使用DSL.inline(String)来防止 jOOQ 为json_build_object()函数参数生成绑定变量:

jsonEntry(inline("name"), field("books.name"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM