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.
Like this:
Like Loading...
Published by lukaseder
I made jOOQ
View all posts by lukaseder
would be nice if this can also be done for table.getIdentity() and runtime meta data. I would like to check which fields provide an autogenerated-id
You’re right, thanks for the suggestion. I’ve created a feature request for this:
https://github.com/jOOQ/jOOQ/issues/4454
thank, i have another one ;-)
it would also be nice to be able to use “table.field(xy).getDataType().nullable()” to check which fields are nullable while working with runtime meta data.. At the moment this always seems to return “true” for runtime meta data.
Tested with jooq-3.6.2
Sure: https://github.com/jOOQ/jOOQ/issues/4456. Feel free to create issues directly on GitHub if you find anything else…