Interface Column.Generator<T>
- Type Parameters:
T- the generated column type
- All Known Subinterfaces:
Column.Generator.Identity<T>, Column.Generator.Preserving<T>
Generators fall into two categories:
- Pre-insert generators - Fetch or generate the column value before the row is inserted
- Post-insert generators - The database automatically sets the column value on insert (identity columns, triggers)
Implementations should override either beforeInsert() or afterInsert():
- If
inserted()returns true, the generated value is included in the insert statement andbeforeInsert(Entity, Column, Database, Connection)should be used - If
inserted()returns false, the database generates the value automatically andafterInsert(Entity, Column, Database, Statement)should be used
The common generators are provided by identity(), sequence(String) and queried(String).
A custom generator, setting a random UUID as the key:
class UUIDGenerator implements Generator<String> {
@Override
public void beforeInsert(Entity entity, Column<String> column, Database database, Connection connection) {
// Only generate if not already set
if (!entity.present(column)) {
entity.set(column, UUID.randomUUID().toString());
}
}
}
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceIndicates an identity column based generator.static interfaceIndicates a preserving column based generator. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidPrepares the given entity after insert, that is, fetches automatically generated values and populates the column value in the entity.static <T> Column.Generator<T> Instantiates a generator which fetches automatically incremented column values after insert.default voidbeforeInsert(Entity entity, Column<T> column, Database database, Connection connection) Prepares the given entity for insert, that is, generates and fetches any required values and populates the column value in the entity.static <T> Column.Generator<T> Returns a generator leaving the column out of the insert, so that its default makes the value, a UUID made bydefault random_uuid()or a time bydefault current_timestampfor example, rather than the application.default booleanSpecifies whether thisColumn.Generatorrelies on the insert statement to return the generated column values via the resultingStatement.getGeneratedKeys()resultSet, accessible inafterInsert(Entity, Column, Database, Statement).static <T> Column.Generator.Identity<T> identity()Returns a column value generator based on an IDENTITY type column.default booleaninserted()The default implementation returns true.static <T> Column.Generator.Preserving<T> preserving(Column.Generator<T> generator) Returns a generator inserting the value the entity holds as it is, the given generator making one where it holds none, or null: a key copied from another database kept, as a row copied to an embedded one, while a row inserted without one gets a key generated.static <T> Column.Generator<T> Instantiates a generator which fetches column values using the given query prior to insert.static <T> Column.Generator<T> Instantiates a generator which fetches column values from a sequence prior to insert.static Column.Generator<UUID> uuid()Returns a generator making a random UUID,UUID.randomUUID(), before insert, for a database without a function making one, SQLite for example, or where the value is wanted without reading it back.
-
Method Details
-
inserted
default boolean inserted()The default implementation returns true.An insert writes the columns the entity holds a value for once
beforeInsert(Entity, Column, Database, Connection)has run, null included, so a generator returning true can leave its column out of an insert by removing its value.- Returns:
- true if the generated value should be included in the insert query when entities using this generator are inserted
-
beforeInsert
default void beforeInsert(Entity entity, Column<T> column, Database database, Connection connection) throws SQLException Prepares the given entity for insert, that is, generates and fetches any required values and populates the column value in the entity. The default implementation does nothing, override to implement.- Parameters:
entity- the entity about to be insertedcolumn- the column which value is being generateddatabase- the databaseconnection- the connection to use- Throws:
SQLException- in case of an exception
-
afterInsert
default void afterInsert(Entity entity, Column<T> column, Database database, Statement statement) throws SQLException Prepares the given entity after insert, that is, fetches automatically generated values and populates the column value in the entity. The default implementation does nothing, override to implement.- Parameters:
entity- the inserted entitycolumn- the column which value is being generateddatabase- the databasestatement- the insert statement- Throws:
SQLException- in case of an exception
-
generatedKeys
default boolean generatedKeys()Specifies whether thisColumn.Generatorrelies on the insert statement to return the generated column values via the resultingStatement.getGeneratedKeys()resultSet, accessible inafterInsert(Entity, Column, Database, Statement). The insert asks for the columns of such generators by name, in the case the database stores an unquoted name in. The default implementation returns false.- Returns:
- true if the generated column values should be returned via the insert statement resultSet
- See Also:
-
sequence
Instantiates a generator which fetches column values from a sequence prior to insert.// Oracle or PostgreSQL sequence City.TYPE.as() .attributes( City.ID.as() .primaryKey() .generator(Generator.sequence("world.city_seq"))) .build(); // Usage - the key is fetched from the sequence before insert Entity city = entities.entity(City.TYPE) .with(City.NAME, "Akureyri") .with(City.COUNTRY_FK, iceland) .with(City.POPULATION, 20_000) .build(); Entity.Key key = connection.insert(city); Integer generatedId = key.get(City.ID);- Type Parameters:
T- the generated column type- Parameters:
sequenceName- the sequence name- Returns:
- a sequence based generator
-
queried
Instantiates a generator which fetches column values using the given query prior to insert.// Custom query-based key generation, such as a function call Invoice.TYPE.as() .attributes( Invoice.ID.as() .primaryKey() .generator(Generator.queried("SELECT chinook.next_invoice_id()"))) .build(); // Usage - the key is fetched before insert Entity invoice = entities.entity(Invoice.TYPE) .with(Invoice.CUSTOMER_FK, customer) .with(Invoice.DATE, LocalDate.now()) .build(); Entity.Key key = connection.insert(invoice); Long generatedId = key.get(Invoice.ID);- Type Parameters:
T- the generated column type- Parameters:
query- a query for retrieving the column value- Returns:
- a query based column generator
-
automatic
Instantiates a generator which fetches automatically incremented column values after insert. Note that H2 and SQL Server do not support this, useidentity()for those.- Type Parameters:
T- the generated column type- Parameters:
valueSource- the value source, whether a sequence or a table name- Returns:
- an auto-increment based column value generator
-
identity
Returns a column value generator based on an IDENTITY type column.The value is read from the insert's// SQL Server, MySQL auto-increment, or similar Customer.TYPE.as() .attributes( Customer.ID.as() .primaryKey() .generator(Generator.identity())) .build(); // Usage - the database generates the key on insert Entity customer = entities.entity(Customer.TYPE) .with(Customer.FIRSTNAME, "John") .with(Customer.LASTNAME, "Doe") .with(Customer.EMAIL, "john@example.com") .build(); Entity.Key key = connection.insert(customer); Long generatedId = key.get(Customer.ID);Statement.getGeneratedKeys(), by the column's name, or the one column there is, whatever the driver names it, SQLite'slast_insert_rowid()for anINTEGER PRIMARY KEYfor example.- Type Parameters:
T- the generated column type- Returns:
- an identity based generated column value generator
- See Also:
-
preserving
Returns a generator inserting the value the entity holds as it is, the given generator making one where it holds none, or null: a key copied from another database kept, as a row copied to an embedded one, while a row inserted without one gets a key generated.An entity copied to be inserted as a new row therefore keeps the key of the one copied, refused as a duplicate, unless cleared,
Entity.Builder.clearPrimaryKey(). An editor's insert of an existing entity leaves its generated values out, a new row whatever the generator.A key given does not advance the database's identity or sequence, so a database where both are inserted must keep the two apart, its identity column accepting a value given,
generated by default as identity, notgenerated always.- Type Parameters:
T- the generated column type- Parameters:
generator- the generator making the value where the entity holds none- Returns:
- a generator preserving a value given
-
columnDefault
Returns a generator leaving the column out of the insert, so that its default makes the value, a UUID made bydefault random_uuid()or a time bydefault current_timestampfor example, rather than the application. A value the entity holds is not inserted, the column's default making it all the same, andpreserving(columnDefault())inserts a value given, as a row copied from another database keeping its UUID.A value which is not part of the primary key comes back with the row where the insert selects it, as
insertSelect()and an editor's insert do. A key, by which the row is selected, is read back from the generated keys the JDBC driver returns, so the driver must return a column made by its default there, as H2's does. Some return identity or auto-increment columns only.- Type Parameters:
T- the generated column type- Returns:
- a generator leaving the value to the column's default
-
uuid
Returns a generator making a random UUID,UUID.randomUUID(), before insert, for a database without a function making one, SQLite for example, or where the value is wanted without reading it back. Each insert gets a new one, replacing a value the entity holds, assequence(String)does, andpreserving(uuid())inserts a value given, as a row copied from another database keeping its UUID.- Returns:
- a random UUID generator
-