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 : jOOQ classes and their usage : CRUD and Updatable Recordsprevious : next

# CRUD Operations with UpdatableRecords

UpdatableRecords are a specific subtype of TableRecord that have primary key information associated with them.

As of jOOQ 1.5, the UpdatableRecord essentially contains three additional methods CRUD (Create Read Update Delete) operations:

// Store any changes made to this record to the database.
// The record executes an INSERT if the PRIMARY KEY is NULL or has been changed. Otherwise, an UPDATE is performed.
int store();

// Deletes the record from the database.
int delete();

// Reflects changes made in the database to this Record
void refresh();

An example lifecycle of a book can be implemented as such:

// Create a new record and insert it into the database
TBookRecord book = create.newRecord(T_BOOK);
book.setTitle("My first book");
book.store();

// Update it with new values
book.setPublishedIn(2010);
book.store();

// Delete it
book.delete();

These operations are very simple utilities. They do not reflect the functionality offered by Hibernate or other persistence managers.

# Performing CRUD on non-updatable records

If the jOOQ code-generator cannot detect any PRIMARY KEY, or UNIQUE KEY on your tables, then the generated artefacts implement TableRecord, instead of UpdatableRecord. A TableRecord can perform the same CRUD operations as we have seen before, if you provide it with the necessary key fields. The API looks like this:

// INSERT or UPDATE the record using the provided keys
int storeUsing(TableField<R, ?>... keys)

// DELETE a record using the provided keys
int deleteUsing(TableField<R, ?>... keys);

// Reflects changes made in the database to this Record
void refreshUsing(TableField<R, ?>... keys);

This is useful if your RDBMS does not support referential constraints (e.g. MySQL's MyISAM), or if you want to store records to an unconstrained view. An example lifecycle of a book without any keys can then be implemented as such:

// Create a new record and insert it into the database
TBookRecord book = create.newRecord(T_BOOK);
book.setTitle("My first book");
book.storeUsing(TBook.ID);

// Update it with new values
book.setPublishedIn(2010);
book.storeUsing(TBook.ID);

// Delete it
book.deleteUsing(TBook.ID);

The jOOQ User Manual. Multiple Pages : jOOQ classes and their usage : CRUD and Updatable Recordsprevious : next

The jOOQ Logo