Hilco, I suggest you hook into the code generation by generating a custom code section in each table: https://www.jooq.org/doc/latest/manual/code-generation/codegen-custom-code
In that section, you can easily automatically generate: - The ID wrapper - The Converter for the ID wrapper Additionaly, you could switch to programmatic code generation and generate a <forcedType> configuration for each of your tables / primary keys to appy the mapping from the primary key column type (and the foreign keys too, of course) to the newly generated ID wrappers and their converters. This will work: - If all your primary keys are single-column keys - If all your foreign key columns reference only a single primary key Hope this helps, Lukas 2017-09-07 17:55 GMT+02:00 Hilco Wijbenga <[email protected]>: > Hey Nikola, > > On 7 September 2017 at 00:53, <[email protected]> wrote: > > Has anyone tried to build type-safe IDs with jOOQ? > > > > Something like described e.g. here: > > https://dysphoria.net/2013/04/12/id-type-safety-in-database-code/ > > https://www.wix.engineering/single-post/on-identifier- > types-type-safety-and-guids > > (there are probably better articles about it - it is an age-old idea) > > > > It looks to me it would not be that hard to do, given jOOQ already > generates > > a bunch of useful code (for example a class for each table) and supports > > custom converters. Then the types would just propagate from records to > the > > whole codebase easily > > > > The only boilerplate could be in XML specifying all the converters. > > > > Am I missing any roadblocks? > > I generate our IDs (one per table) by iterating through the fields in > the Tables class (i.e. the one generated by jOOQ). That makes it easy > to keep everything up-to-date. The mappings from value object to > Record (and back) are manually generated. Automating that is > substantially more complex. > > I haven't gone quite so far as to actually change the jOOQ generated > code but, now that you mention it, I really should do that too. That > would also simplify the mappings. The problem, at least with my > current setup, is that I now have a chicken-and-egg problem. I use the > jOOQ generated code to generate the IDs but the converters (assuming > there is one per ID) would have to be generated too ... before I even > have the Tables class. > > I believe there is some sort of Metadata object that would probably > work better but, if memory serves, Lukas mentioned that it wasn't > always reliable. I'll have to give it some thought. > > Cheers, > Hilco > > -- > You received this message because you are subscribed to the Google Groups > "jOOQ User Group" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "jOOQ User Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
