Over a million developers have joined DZone.

jOOQ Tip of the Day: Discover all Primary Keys

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

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

GENERATED_SCHEMA
    .getTables()
    .stream()
    .map(t -> t.getPrimaryKey())
    .filter(Objects::nonNull)
    .forEach(System.out::println);

Whereas with Java 7 or less, you’d write

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

Using runtime meta data

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

DSL.using(configuration)
   .meta()
   .getTables()
   .[ same as above ]

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



Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Published at DZone with permission of Lukas Eder, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}