Naming convention in auto-generated code

77 views
Skip to first unread message

Gang Luo

unread,
Sep 16, 2021, 12:28:53 PM9/16/21
to jOOQ User Group
Hi folks,

One of the file auto-generated by JOOQ is "Indexes.java". Inside it there are primary keys defined like "PRIMARY_KEY_302". That "302" in the name seems a bit random to me. I am wondering where does it come from. Of course to say it is random is incorrect, as I rerun the code gen multiple times it alway produce the same variable names. 

The reason I ask is because I am generating two set of code from two different DB schema, A and B. Both schema has exactly the same tables and specs/schema, so I expect the code generated from them, specifically for that "Indexes.java", will be the same. The reality is, they are 99% the same. For that 1% difference they are actually "similar" but just not the same. Following are the example of 3 variables generated from the schema A and B that don't match exactly.

PRIMARY_KEY_EE -- PRIMARY_KEY_E

PRIMARY_KEY_3 -- PRIMARY_KEY_30

PRIMARY_KEY_30 -- PRIMARY_KEY_302

I am wondering why is that and how can I have both generate the exactly same variables in "Indexes.java".

Thanks.

-Gang

Lukas Eder

unread,
Sep 16, 2021, 1:53:43 PM9/16/21
to jooq...@googlegroups.com
You're probably using H2 or some implementation that uses H2 behind the scenes, like DDLDatabase or JPADatabase? You could implement your own generator strategy:


Or use your actual target database instead for code generation.
Thanks,
Lukas

--
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 jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/4ae90136-0831-402e-add7-970c6e8228c9n%40googlegroups.com.

Jens Teglhus Møller

unread,
Sep 17, 2021, 4:01:48 AM9/17/21
to jOOQ User Group
In a project I'm working on we model our database in JPA and then generate the JOOQ model by programmatically setting up an EntityManager that initializes the database in H2 and then pass the data source to the JooqGenerator.

Before running the generator we have various code that fixes small issues like this, it is very easy using jooqs meta feature:

private void fixPrimaryKeyNames(DSLContext jooq) {
  Meta meta = jooq.meta().filterSchemas(s -> s.getName().equals("public"));
  meta.getTables().forEach(t -> {
    String newName = "pk_" + t.getName();
    jooq
      .alterTable(t)
      .renameConstraint(t.getPrimaryKey().constraint())
      .to(newName)
      .execute();
  });
}

Best regards Jens

Gang Luo

unread,
Sep 21, 2021, 7:44:03 AM9/21/21
to jOOQ User Group
Thanks Lukas for the context, and Jens for the work around. 
Reply all
Reply to author
Forward
0 new messages