最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Fetch Single Table into Nested Pojo - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论