DAO generation being skipped

240 views
Skip to first unread message

rathip...@gmail.com

unread,
May 10, 2018, 3:57:13 AM5/10/18
to jOOQ User Group
I've a table that does not have a primary key but a composite key that is unique. In the absence of a primary key, the code in JavaGenerator.generateDao() is skipping DAO generation. Shouldn't it instead attempt to pick the unique key, if present, as shown below? Also, I recommend that a warn message be generated instead of an info message under such circumstances.

        UniqueKeyDefinition key = table.getPrimaryKey();
        if (key == null) {
            if (table.getUniqueKeys().isEmpty()){
                Log.warn("Skipping DAO generation", out.file().getName());
                return;
            } else {
                key = table.getUniqueKeys().get(0);
            }
        }

Lukas Eder

unread,
May 11, 2018, 5:11:45 AM5/11/18
to jooq...@googlegroups.com
Yeah, heh, "the unique key" - but which one? A table can only have one "primary key", thus the name "primary". But it can have any number of "unique" keys, so it would be unwise to just pick any key at random.

You can, however, specify "override primary keys" or "synthetic primary keys" in the absence of actual primary keys:

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


P R

unread,
May 15, 2018, 1:49:59 PM5/15/18
to jOOQ User Group
Thank you for the pointers. I was expecting that if there is no primary key but only one unique key, the generator would pick it by default. If there are multiple unique keys, I understand that one cannot pick at random. Anyways, I tried specifying via configuration and still didn't see DAO being generated. Perhaps, missing something basic on my end. Here's what my table definition looks like in the Postgres database.

CREATE TABLE public."PersonEmail"
(
  "personId" character(8) NOT NULL,
  email character varying(64) NOT NULL,
  "contactTypeId" smallint NOT NULL,
  CONSTRAINT "PersonEmail_personId_contactTypeId_key" UNIQUE ("personId", "contactTypeId")
)

And here are the two configurations that I tried independently.

<syntheticPrimaryKeys>public\.PersonEmail\.COLUMN(1|3)</syntheticPrimaryKeys>

<overridePrimaryKeys>PersonEmail_personId_contactTypeId_key</overridePrimaryKeys>

Appreciate if you could point me to what's wrong in the above configurations? I also presume, I can have multiple entries, each delimited by "|". 

Lukas Eder

unread,
May 16, 2018, 3:31:19 AM5/16/18
to jooq...@googlegroups.com
2018-05-15 19:49 GMT+02:00 P R <rathip...@gmail.com>:
Thank you for the pointers. I was expecting that if there is no primary key but only one unique key, the generator would pick it by default. If there are multiple unique keys, I understand that one cannot pick at random.

That would be an option (jOOQ 1.0 did this), but it might be surprising to some people as well. After all, what's the point of not having a primary key in the first place? Especially in your case, the example below is a many-to-many relationship table, and it is good practice to make the two foreign keys part of the composite primary key...
 
Anyways, I tried specifying via configuration and still didn't see DAO being generated.

Were the keys generated (in Keys.java)? And referenced in PersonEmail.getPrimaryKey()?
 
Perhaps, missing something basic on my end. Here's what my table definition looks like in the Postgres database.

CREATE TABLE public."PersonEmail"
(
  "personId" character(8) NOT NULL,
  email character varying(64) NOT NULL,
  "contactTypeId" smallint NOT NULL,
  CONSTRAINT "PersonEmail_personId_contactTypeId_key" UNIQUE ("personId", "contactTypeId")
)

And here are the two configurations that I tried independently.

<syntheticPrimaryKeys>public\.PersonEmail\.COLUMN(1|3)</syntheticPrimaryKeys>

What's this COLUMN(1|3)? That seems to have been copy-pasted from the manual, right? It should be:
 
<syntheticPrimaryKeys>public\.PersonEmail\.(personId|contactTypeId)</syntheticPrimaryKeys>

<overridePrimaryKeys>PersonEmail_personId_contactTypeId_key</overridePrimaryKeys>

That's interesting, this works for me. What happens if you fully qualify that name (e.g. prefixing it by .*)?
 
Appreciate if you could point me to what's wrong in the above configurations? I also presume, I can have multiple entries, each delimited by "|". 

Yes, it's an ordinary Java regular expression

P R

unread,
May 16, 2018, 12:50:30 PM5/16/18
to jOOQ User Group
I did this first.


<syntheticPrimaryKeys>public\.PersonEmail\.(personId|contactTypeId)</syntheticPrimaryKeys>

But that didn't work. Hence looked at the example again and thought perhaps I've to specify the column index instead i.e. COLUMN(1|3). That didn't work too. Then I tried

<overridePrimaryKeys>PersonEmail_personId_contactTypeId_key</overridePrimaryKeys>

which did not work either. Well now I'm able to generate the DAO with the above configurations. The only reasoning I can think of why it was not working earlier might have to do with a stale version of the configuration file being picked up from the "resource" folder, which is automatically copied to the class path by maven. I ran the "clean" task yesterday, for other reasons, and that may have inadvertently solved the issue after I retried since your reply.  Thank you so much for your help and guidance. I'll definitely look into the composite primary key suggestion.
Reply all
Reply to author
Forward
0 new messages