Interface Column.Generator<T>

Type Parameters:
T - the generated column type
All Known Subinterfaces:
Column.Generator.Identity<T>, Column.Generator.Preserving<T>
Enclosing interface:
Column<T>

public static interface Column.Generator<T>
Generates column values for entities on insert.

Generators fall into two categories:

  1. Pre-insert generators - Fetch or generate the column value before the row is inserted
  2. Post-insert generators - The database automatically sets the column value on insert (identity columns, triggers)

Implementations should override either beforeInsert() or afterInsert():

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 Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Indicates an identity column based generator.
    static interface 
    Indicates a preserving column based generator.
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    afterInsert(Entity entity, Column<T> column, Database database, Statement statement)
    Prepares the given entity after insert, that is, fetches automatically generated values and populates the column value in the entity.
    static <T> Column.Generator<T>
    automatic(String valueSource)
    Instantiates a generator which fetches automatically incremented column values after insert.
    default void
    beforeInsert(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 by default random_uuid() or a time by default current_timestamp for example, rather than the application.
    default boolean
    Specifies whether this Column.Generator relies on the insert statement to return the generated column values via the resulting Statement.getGeneratedKeys() resultSet, accessible in afterInsert(Entity, Column, Database, Statement).
    Returns a column value generator based on an IDENTITY type column.
    default boolean
    The default implementation returns true.
    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>
    queried(String query)
    Instantiates a generator which fetches column values using the given query prior to insert.
    static <T> Column.Generator<T>
    sequence(String sequenceName)
    Instantiates a generator which fetches column values from a sequence prior to insert.
    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 inserted
      column - the column which value is being generated
      database - the database
      connection - 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 entity
      column - the column which value is being generated
      database - the database
      statement - the insert statement
      Throws:
      SQLException - in case of an exception
    • generatedKeys

      default boolean generatedKeys()
      Specifies whether this Column.Generator relies on the insert statement to return the generated column values via the resulting Statement.getGeneratedKeys() resultSet, accessible in afterInsert(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

      static <T> Column.Generator<T> sequence(String sequenceName)
      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

      static <T> Column.Generator<T> queried(String query)
      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

      static <T> Column.Generator<T> automatic(String valueSource)
      Instantiates a generator which fetches automatically incremented column values after insert. Note that H2 and SQL Server do not support this, use identity() 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

      static <T> Column.Generator.Identity<T> identity()
      Returns a column value generator based on an IDENTITY type column.
      // 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);
      The value is read from the insert's Statement.getGeneratedKeys(), by the column's name, or the one column there is, whatever the driver names it, SQLite's last_insert_rowid() for an INTEGER PRIMARY KEY for example.
      Type Parameters:
      T - the generated column type
      Returns:
      an identity based generated column value generator
      See Also:
    • preserving

      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.

      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, not generated 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

      static <T> Column.Generator<T> columnDefault()
      Returns a generator leaving the column out of the insert, so that its default makes the value, a UUID made by default random_uuid() or a time by default current_timestamp for example, rather than the application. A value the entity holds is not inserted, the column's default making it all the same, and preserving(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

      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. Each insert gets a new one, replacing a value the entity holds, as sequence(String) does, and preserving(uuid()) inserts a value given, as a row copied from another database keeping its UUID.
      Returns:
      a random UUID generator