Jooq dsl one to many relations

Viewed 231

I'm using Spring Data + Jooq DSL. As result entity I'm using not jooq generated entity, but simple one, without any annotations and for relations One To Many getting result:

[{
   "id":2,
   "name":"James",
   "addresses":[
      {
         "id":null,
         "country":null,
         "street":null
      }
   ]
}]

Is any way to return an empty array for addresses?

My code to perform a request:

    public Set<User> getUserById(Set<Long> id) {
        Set<User> result = new HashSet<>();
        ResultQuery users = dsl.select(
                field("u.id", Long.class).as("id"),
                field("u.name", String.class).as("name"),
                field("a.id", Long.class).as("addresses_id"),
                field("a.country", String.class).as("addresses_country"),
                field("a.street", String.class).as("addresses_street")
        ).from("schema.user_table u")
                .leftJoin("schema.address_table a")
                .on("u.id = a.user_id")
                .where(field("u.id").in(id));
        try(ResultSet rs = users.fetchResultSet()) {
            JdbcMapper<User> mapper = JdbcMapperFactory
                    .newInstance()
                    .addKeys("id")
                    .newMapper(User.class);
            result = mapper.stream(rs).collect(Collectors.toSet());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
2 Answers

Why not just use SQL/JSON to produce JSON documents directly from within your database?

public String getUserById(Set<Long> id) {
    return dsl.select(coalesce(jsonArrayAgg(
        jsonObject(
            key("id").value(field("u.id", Long.class)),
            key("name").value(field("u.name", String.class)),
            key("addresses").value(coalesce(
                jsonArrayAgg(jsonObject(
                    key("id").value(field("a.id", Long.class)),
                    key("country").value(field("a.country", String.class)),
                    key("street").value(field("a.street", String.class))
                )),
                jsonArray()
            ))
        ),
        jsonArray()
    )))
    .from("schema.user_table u")
    .leftJoin("schema.address_table a")
    .on("u.id = a.user_id")
    .where(field("u.id").in(id))
    .fetchSingle().value1().data();
}

If you really need the intermediate User representation, then you can either:

public Set<User> getUserById(Set<Long> id) {
    return dsl.select(
        field("u.id", Long.class),
        field("u.name", String.class),
        multisetAgg(
            field("a.id", Long.class),
            field("a.country", String.class),
            field("a.street", String.class)
        ).convertFrom(r -> r == null 
            ? Collections.<Address>emptyList() 
            : r.map(Records.mapping(Address::new)))
    )
    .from("schema.user_table u")
    .leftJoin("schema.address_table a")
    .on("u.id = a.user_id")
    .where(field("u.id").in(id))
    .fetchSet(Records.mapping(User::new));
}

Side note on code generation and execution

While not strictly relevant to this question, unless your schema is dynamic (not known at compile time), I really urge you to reconsider using source code generation. If you're not using it, you're missing out on a lot of jOOQ API advantages, just like when you're executing a jOOQ query with something other than jOOQ.

For me worked specify as a key addressId:

.addKeys("id", "addresses_id")
Related