javasqlaliasjooq

How to use alias in jOOQ


Could someone please guide me on how to use alias in jOOQ. I tried looking into jOOQ documentation but it is not clear. Please provide an example if possible.


Solution

  • Both org.jooq.Table and org.jooq.Field types implement org.jooq.AliasProvider. This means, that you can call as(String) on them, to create an aliased object. Example:

    Table<?> aliasedTable = MY_TABLE.as("t");
    Field<?> aliasedField = MY_FIELD.as("f");
    

    The examples from the jOOQ manual include:

    TBook book = T_BOOK.as("b");
    TAuthor author = T_AUTHOR.as("a");
    
    create.select(author.ID, book.ID)
          .from(author)
          .join(book).on(author.ID.equal(book.AUTHOR_ID))