Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JOOQ just to store table/column names and types with no regard to Record types

Tags:

java

jooq

I'm currently evaluating JOOQ because I believe I started reinventing the wheel which looks very close to part of JOOQ :)

Now, while digging in great JOOQ documentation I've found that my use case lies somewhere between Using JOOQ as SQL Builder and Using JOOQ as SQL Builder with Code generation i.e. I would like to:

  1. Create plain SQL strings like it is shown in Using JOOQ as SQL Builder part
  2. Instead of using hard-coded DSL.fieldByName("BOOK","TITLE") constructs, I prefer storing name of a table along with it's column names and types like it's shown in Using JOOQ as SQL Builder with Code generation part
  3. I prefer not to use code generation (at least not on regular basis), but rather creating TableImpl myself when new table is needed.

While digging in manual, I've found how table implementation should look like in chapter Generated tables. However, TableImpl class as well as Table interface should be parameterized with record type and the same goes for TableField class. I believe this is done for easier type inference when directly querying database and retrieving results, though I may be mistaken.

So my questions are:

  1. Is there a guide in manual on how to create Table and TableField implementations? Or I can simply generate them once for my database schema and use generated code as a guideline?
  2. How can I gracefully "discard" record type parameters in implemented classes? First, I thought about using java.lang.Void class as type parameter but then I noticed that only subclasses of Record are allowed... The reason is that I don't need record types at all because I plan to use generated by JOOQ SQL queries in something like Spring JdbcTemplate so mapping is done by myself.

Thanks in advance for any help!

like image 799
Yuriy Nakonechnyy Avatar asked Sep 16 '25 10:09

Yuriy Nakonechnyy


1 Answers

Given your use-case, I'm not sure why you'd like to roll your own Table and TableField implementations rather than using the ones generated by jOOQ. As you stated yourself, you don't have to regenerate that code every time the DB schema changes. Many users will just generate the schema once in a while and then put the generated artefacts under version control. This will help you keep track of newly added changes.

To answer your questions:

  1. Yes, there are some examples around the use of CustomTable. You may also find some people sharing similar experiences on the user group
  2. Yes you can just use Record. Your minimal custom table type would then be:

    class X extends TableImpl<Record> {
        public X() {
            super("x");
        }
    }
    

    Note that you will be using jOOQ's internal API (TableImpl), which is not officially supported. While I'm positive that it'll work, it might break in the future, e.g. as the super constructor signature might change.

like image 195
Lukas Eder Avatar answered Sep 19 '25 02:09

Lukas Eder