Код: Выделить всё
GET /foo/123
GET /foo/123?include=bars&include=baz
< /code>
, где Foo, Bar и Baz являются типами ресурсов в модели, каждый из которых представлен таблицей. Foo Might Have Baz, и Foo Has Many Bar, так что это то, на что может выглядеть ресурс Foo, когда все включено: < /p>
{"id": 123,
"fooname": "the foo",
"and more": "foo fields",
"baz_id": 456,
"baz": {"id": 456, "and more": "baz fields"},
"bars": [{"id": 789, "foo_id": 123, "and more": "bar fields"},
{...}]}
Теперь, чтобы построить jooq Query: < /p>
Код: Выделить всё
// I will also have to build the Page version of this for returning multiple Foos,
// with the same kind of issues. note not using the generated FooRecord because we
// do not need all the fields in the response
Foo foo = this.dsl.select(
some, foo, fields,
include.contains("baz") ? BAZ_INCLUDE : null,
include.contains("bars") ? BARS_INCLUDE : null)
.from(FOOS)
.where(FOOS.ID.equal(fooId))
.fetchSingle(Records.mapping(Foo::new));
Код: Выделить всё
// we're actually using the Foo table's Bar and Baz relations here since they carry
// the information about the relationship, and will cause an implicit join, which
// saves some work when building the query. e.g. FOO.bar().ID
// Foo has 0 or 1 Baz
private static SelectField BAZ_INCLUDE = row(some, baz, fields)
.mapping(Baz::new)
.as("baz");
// Foo has 0 to N Bars
private static SelectField BARS_INCLUDE = multiset(select(some, bar, fields))
.convertFrom(r -> r.map(Records.mapping(Bar::new))
.as("bars");
< /code>
И чудесным образом, это работает! ... при использовании полной проекции (include={baz,bars}Как я могу сделать динамические проекции между соединениями и все еще получаю характеристики хорошего типа, так что я разумно уверен, что я не забыл полю в записи Foo или в выборе? Из последних вещей мне нужно достичь паритета функции с нашим существующим инструментом Hibernate+QueryDsl+EntityGraph (а также улучшить некоторые вещи над этим!).
Подробнее здесь: https://stackoverflow.com/questions/797 ... ws-in-jooq
Мобильная версия