【问题标题】:JOOQ: how to use multisetJOOQ:如何使用多重集
【发布时间】:2022-01-08 07:52:54
【问题描述】:

鉴于这 3 个具有多对多关系的表

book_id title isbn num_pages
1 JOOQ 1234 123
2 SQL 2345 155

book_author

book_id author_id
1 1
2 1
2 2

作者

author_id author_name
1 Lucas
2 Jose

还有记录

public record Book(Integer id, String title, String isbn, List<Author> authors) {}// don't need nr of pages

public record Author(Integer id, String name){}

如何使用多重集获取List&lt;Book&gt;

List<Book> books = dsl.select(
        BOOK_AUTHOR.BOOK_ID,
        select(BOOK.TITLE).from(BOOK).where(BOOK.BOOK_ID.eq(BOOK_AUTHOR.BOOK_ID)).asField("title"),
        select(BOOK.ISBN).from(BOOK).where(BOOK.BOOK_ID.eq(BOOK_AUTHOR.BOOK_ID)).asField("isbn"),
        multiset(
                selectFrom(AUTHOR).where(AUTHOR.AUTHOR_ID.eq(BOOK_AUTHOR.AUTHOR_ID))
        ).as("authors").convertFrom(record -> record.map(Records.mapping(Author::new)))
).from(BOOK_AUTHOR)
 .where(BOOK_AUTHOR.BOOK_ID.in(1, 2))
 .fetchInto(Book.class);

 books.forEach(System.out::println);

预期的列表包含 2 本书,但得到 3 本书!?...

Book{id=1, title='JOOQ', isbn='1234', authors=[Author{name='Lucas'}]}
Book{id=2, title='SQL', isbn='2345', authors=[Author{name='Lucas'}]}
Book{id=2, title='SQL', isbn='2345', authors=[Author{name='Jose'}]}

有什么问题?

生成的SQL:

select "public"."book_author"."book_id", (select "public"."book"."title" from "public"."book" where "public"."book"."book_id" = "public"."book_author"."book_id") as "title", (select "public"."book"."isbn" from "public"."book" where "public"."book"."book_id" = "public"."book_author"."book_id") as "isbn", (select coalesce(jsonb_agg(jsonb_build_array("v0", "v1")), jsonb_build_array()) from (select "public"."author"."author_id" as "v0", "public"."author"."author_name" as "v1" from "public"."author" where "public"."author"."author_id" = "public"."book_author"."author_id") as "t") as "authors" from "public"."book_author" where "public"."book_author"."book_id" in (1, 2)

使用:

  • JAVA 17
  • POSTGRESQL
  • spring-boot (2.6.1) 与 spring-boot-starter-jooq
  • JOOQ 3.15.4(允许使用multiset 函数)
  • JOOQ 代码生成(maven 插件)

【问题讨论】:

    标签: postgresql spring-boot jooq


    【解决方案1】:

    问题是您从有 3 个条目的 BOOK_AUTHOR 开始。您应该从 BOOK 中选择:

    List<Book> books = dsl.select(
       BOOK.BOOK_ID,
       BOOK.TITLE,
       BOOK.ISBN,
       multiset(
         select(AUTHOR.AUTHOR_ID,AUTHOR.AUTHOR_NAME)
             .from(AUTHOR).innerJoin(BOOK_AUTHOR)
             .on(BOOK_AUTHOR.AUTHOR_ID.eq(AUTHOR.AUTHOR_ID))
             .where(BOOK_AUTHOR.BOOK_ID.eq(BOOK.BOOK_ID))
        ).as("authors")
         .convertFrom(record -> record.map(Records.mapping(Author::new)))
        ).from(BOOK)
         .where(BOOK.BOOK_ID.in(1, 2))
         .fetchInto(Book.class);
    
    

    【讨论】:

    • 谢谢。我收到这段代码的错误......但我明白你的意思
    • 经过一些小改动后工作正常。谢谢!
    • 很高兴您发现了问题
    猜你喜欢
    • 2023-01-18
    • 2019-07-14
    • 2022-01-17
    • 2019-12-17
    • 2019-10-08
    • 2017-08-12
    • 2013-12-05
    • 2018-07-11
    • 2013-10-24
    相关资源
    最近更新 更多