This version of the manual is outdated. For the latest version, follow this link: http://www.jooq.org/doc/3.0/manual.

The jOOQ User Manual. Multiple Pages : Meta model code generation : Tables, views and their recordsprevious : next

# Tables and TableRecords

The most important generated artefacts are Tables and TableRecords. As discussed in previous chapters about Tables and Results, jOOQ uses the Table class to model entities (both tables and views) in your database Schema. Every Table has a Record type associated with it that models a single tuple of that entity: Table<R extends Record>. This couple of Table<R> and R are generated as such:

Suppose we have the tables as defined in the example database. Then, using a default configuration, these (simplified for the example) classes will be generated:

# The Table as an entity meta model

public class TAuthor extends UpdatableTableImpl<TAuthorRecord> {

    // The singleton instance of the Table
    public static final TAuthor T_AUTHOR = new TAuthor();

    // The Table's fields.
    // Depending on your jooq-codegen configuraiton, they can also be static
    public final TableField<TAuthorRecord, Integer> ID =            // [...]
    public final TableField<TAuthorRecord, String> FIRST_NAME =     // [...]
    public final TableField<TAuthorRecord, String> LAST_NAME =      // [...]
    public final TableField<TAuthorRecord, Date> DATE_OF_BIRTH =    // [...]
    public final TableField<TAuthorRecord, Integer> YEAR_OF_BIRTH = // [...]

    // When you don't choose the static meta model, you can typesafely alias your tables.
    // Aliased tables will then hold references to the above final fields, too
    public TAuthor as(String alias) {
      // [...]
    }
}

# The Table's associated TableRecord

If you use the SimpleSelectQuery syntax (both in standard and DSL mode), then your SELECT statement will return the single Table<R extends Record>'s associated Record type <R>. In the case of the above TAuthor Table, this will be a TAuthorRecord.

public class TAuthorRecord extends UpdatableRecordImpl<TAuthorRecord> {

    // Getters and setters for the various fields
    public void setId(Integer value) {       // [...]
    public Integer getId() {                 // [...]
    public void setFirstName(String value) { // [...]
    public String getFirstName() {           // [...]
    public void setLastName(String value) {  // [...]
    public String getLastName() {            // [...]
    public void setDateOfBirth(Date value) { // [...]
    public Date getDateOfBirth() {           // [...]

    // Navigation methods for foreign keys
    public List<TBookRecord> fetchTBooks() { // [...]
}

# Generated or custom POJO's instead of jOOQ's Records

If you're using jOOQ along with Hibernate / JPA, or if you want to use your own, custom domain-model instead of jOOQ's Record type-hierarchy, you can choose to select values into POJOs. Let's say you defined a POJO for authors:

package com.example;

import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
public class MyAuthor {
    // Some fields may be public
    @Column(name = "ID")
    public int id;

    // Others are private and have associated getters / setters:
    private String firstName;
    private String lastName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "FIRST_NAME")
    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name = "LAST_NAME")
    public String getLastName() {
        return lastName;
    }
}

The above could be your custom POJO or a POJO generated by jooq-codegen (see the manual's section about advanced codegen configuration for more details). Also, JPA-annotations are not necessary if you wish to let jOOQ map record columns onto your POJO attributes by convention. Instead of fetching records normally, you can now let jOOQ fetch records "into" your custom type:

List<MyAuthor> results = create.select().from(TAuthor.T_AUTHOR).fetchInto(MyAuthor.class);

Read the javadoc for Record.into() for more details.


The jOOQ User Manual. Multiple Pages : Meta model code generation : Tables, views and their recordsprevious : next

Fork me on GitHub
The jOOQ Logo