java - Fetch Single Table into Nested Pojo - Stack Overflow

I have the following database table:catalog- author- book_name- publication_dateValues in the catal

I have the following database table:

catalog
- author
- book_name
- publication_date

Values in the catalog table look something like

author book_name publication_date
Mark Smith How to Read 2024-01-01
Sarah Doe A Book 1826-01-01
Sarah Doe A Second Book 1855-07-15

I have the following database table:

catalog
- author
- book_name
- publication_date

Values in the catalog table look something like

author book_name publication_date
Mark Smith How to Read 2024-01-01
Sarah Doe A Book 1826-01-01
Sarah Doe A Second Book 1855-07-15

I want to fetch the table into a Catalog POJO similar to this one:

public class Catalog {
    private String author;
    List < BookEntry > book_entries;
}

public class BookEntry {
    private String bookName;
    private Date publicationDate;
}

I have used the multiset (https://www.jooq./doc/latest/manual/sql-building/column-expressions/multiset-value-constructor/) operator for doing similar things across separate tables, but I don't know if it is appropriate for this particular use-case.

Share Improve this question asked Nov 20, 2024 at 20:56 ConnerConner 6757 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The simplest way seems to be MULTISET_AGG(), in this case:

ctx.select(CATALOG.AUTHOR,
        multisetAgg(CATALOG.BOOK_NAME, CATALOG.PUBLICATION_DATE)
        .convertFrom(r -> r.map(Records.mapping(BookEntry::new))))
   .from(CATALOG)
   .groupBy(CATALOG.AUTHOR)
   .fetch(Records.mapping(Catalog::new))

Assuming you have the appropriate constructors on those classes, of course, and that you can handle the perils of not normalising this schema.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742328602a4423255.html

相关推荐

  • java - Fetch Single Table into Nested Pojo - Stack Overflow

    I have the following database table:catalog- author- book_name- publication_dateValues in the catal

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信