Over a million developers have joined DZone.

jOOQ Tip of the Day: Discover all Primary Keys

DZone's Guide to

jOOQ Tip of the Day: Discover all Primary Keys

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

We’ve recently encountered this interesting use-case on the jOOQ user group. How do you discover all primary keys of your schema via the jOOQ API? There are two ways:

  • Using the generated meta data
  • Using runtime meta data

Let’s see how it works:

Using the generated meta data

This is straightforward. If you’re using Java 8, you can run the following program

    .map(t -> t.getPrimaryKey())

Whereas with Java 7 or less, you’d write

for (Table<?> t : GENERATED_SCHEMA.getTables()) {
    UniqueKey<?> key = t.getPrimaryKey();
    if (key != null)

Using runtime meta data

If you’re not using the code generator, the same can be achieved with:

   .[ same as above ]

That’s it! Another interesting use case of querying the meta information via the jOOQ API can be seen here.

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}