0
|Table1|  1           n  |Table2| 1            n   |Table 3|
|      | --------------> |      | -------------->  |       |
|      |                 |      |                  |       |
--------                  --------                  --------
(experiment)              (tags)                  (TagDescription)

I am trying to map the data from the above three one-to-many linked tables to the DTOs below. However, the Jooq query is not working as expected. Values of table 1 and table 3 are mapped correctly but table 2 values are all Null. Here are the DTOs.

class ExperimentDto {
  long id;
  String name;
  List<TagDto> tags;
}

class TagDto {
  long id;
  long experimentId;
  String name;
  List<TagDescriptionDto> tagDescription;
}

class TagDescriptionDto {
  long id;
  long tadId;
  String description;
}

... and here is my query. I took reference from this answer to write my query.

dslContext.select(
    Experiment.asterisk(), // map all the Experiment table field to DTO
    field(
        select(
            jsonArrayAgg(jsonObject(
                jsonEntry(jsonObject(Tag.fields())),  // map all the Tag table field to DTO + ONE-to-Many with TagDescription but these fields are all NULL
                jsonEntry("tagDescription", field(
                        select(jsonArrayAgg(jsonObject(TagDescription.fields())))
                            .from(orderDetailAddition)
                            .where(TagDescription.ID.eq(Tag.ID))))
                )
            )
        ).from(Tag)
         .where(Tag.eq(Experiment.ID))
    ).as("tags")
).from(Experiment)
.where(Experiment.NAME.eq("Name"))
.fetchInto(ExperimentDto.class);

Could somebody give me hint what is missing and wrong in the query. Apparently, this part have some issues jsonEntry(jsonObject(Tag.fields())).

Moreover, is it a good way (jsonmappers in query) to read and map such relations on DTO or we can do it in a better way using Jooq? (can't use multiset because I am on 3.14)

Update: Updated to JOOQ 3.15.4 and started using MULTISET, but now I am getting BAD SQL exceptions on the simplest query. I wrote this query that is throwing an error. For some reason, it is unable to find experiment.id inside the nested query.

SELECT 
    experiment.id,
    experiment.name,
    (SELECT 
            COALESCE(JSON_MERGE_PRESERVE('[]',
                                CONCAT('[',
                                        GROUP_CONCAT(JSON_ARRAY(v0)
                                            SEPARATOR ','),
                                        ']')),
                        JSON_ARRAY())
        FROM
            (SELECT 
                tags.name AS v0
            FROM
                tags
            WHERE
                (tags.experiment_id = experiment.id)) AS t) AS tags
FROM
    experiment
WHERE
    experiment.id > 0;

Error Code: 1054. Unknown column 'experiment.id' in 'where clause'

MySQL version: 5.7.35

ihaider
  • 1,290
  • 4
  • 19
  • 38
  • *"can't use multiset because I am on 3.14"* Why not upgrade to 3.15? Things will get a lot easier and type safe with multiset... – Lukas Eder Nov 23 '21 at 19:24
  • When I updated it to 3.15.4, I got this error `Failed to configure a ConnectionFactory: 'url' attribute is not specified` and application is not booting. Couldn't get a chance to check the issue in detail. Do we have some breaking changes in 3.15 and do we need to change something related to DB connection? When I revert back to 3.14, everything starts working as before – ihaider Nov 23 '21 at 21:03
  • Nevermind, I got the answer here (https://stackoverflow.com/questions/68297295/how-to-fix-consider-defining-a-bean-of-type-org-jooq-dslcontext-in-your-confi). I have one question though. For some database columns, the exported field is now suffixed with and a `_`. It doesn't happen in 3.14. Could you please mention what is this about? Thanks – ihaider Nov 23 '21 at 22:19
  • Moreover, after updating to `3.15.14`, I am not getting Bad SQL exceptions. Again, the same query was working fine for the previous version I was using. This is the exception -> `Translating SQLException with SQL state '42000', error code '1064', message [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set @@group_concat_max_len = 4294967295; select `b`.`batch_id`, (select coalesce' at line 1]; ` – ihaider Nov 23 '21 at 23:08
  • Can you pleas ask separate questions? It's very hard to provide meaningful answers if you edit this question with new content – Lukas Eder Nov 24 '21 at 07:56
  • ... anyway. I'll be happy to answer specific questions. As the question is phrased right now, it looks more like a log of your findings, so I don't know what open questions remain – Lukas Eder Nov 25 '21 at 10:35

0 Answers0