Class IdGenerator

java.lang.Object
org.fastnate.generator.context.IdGenerator
Direct Known Subclasses:
IdentityValue, SequenceIdGenerator, TableIdGenerator

public abstract class IdGenerator extends Object
Saves the current value and increments the value for a GeneratedIdProperty.
Author:
Tobias Liefke
  • Constructor Details

    • IdGenerator

      public IdGenerator()
  • Method Details

    • addNextValue

      public abstract void addNextValue(TableStatement statement, GeneratorColumn column, Number nextValue)
      Adds the generated value to the given statement. Depending on the generator this may add the value itself or a reference to the value.
      Parameters:
      statement - the current insert statement
      column - the ID column of the current table
      nextValue - the current value of the column, previously generated with createNextValue()
    • alignNextValue

      public abstract void alignNextValue(StatementsWriter writer) throws IOException
      Creates all statements that are necessary to set the next value created from the database is currentValue + 1.
      Parameters:
      writer - the target of the created statements
      Throws:
      IOException - if the writer throws one
    • createNextValue

      protected abstract long createNextValue()
      Resolves the next value of this generator.
      Returns:
      the generated value
    • createNextValue

      public <N extends Number> N createNextValue(Class<N> propertyClass)
      Resolves the next value of this generator.
      Parameters:
      propertyClass - the type of the generated value
      Returns:
      the generated value
    • createPreInsertStatements

      public void createPreInsertStatements(StatementsWriter writer) throws IOException
      Creates the statements that are needed in the output before addNextValue(TableStatement, GeneratorColumn, Number).
      Parameters:
      writer - target for the created statements
      Throws:
      IOException - if the writer throws one
    • derive

      public IdGenerator derive(GeneratorTable entityTable)
      Some implementations (like the Hibernate table generator) create a different generator, depending on the table name.
      Parameters:
      entityTable - the current entity table
      Returns:
      the generator
    • getCurrentValue

      public abstract long getCurrentValue()
      The last value returned by createNextValue(Class).
      Returns:
      the current value
    • getExpression

      public abstract ColumnExpression getExpression(GeneratorTable table, GeneratorColumn column, Number id, boolean whereExpression)
      Builds the reference to another entity that has the given ID.
      Parameters:
      table - the main table of the entity
      column - the name of the column of the ID
      id - the current value of the ID
      whereExpression - indicates if this expression is needed for a "SELECT ... WHERE ..." - some dialects behave differently in this situation
      Returns:
      the expression for selecting the ID
    • isPostIncrement

      public abstract boolean isPostIncrement()
      Indicates that createNextValue(Class) should be called after the entity was written - as the value is not available before.
      Returns:
      true if the database increments the value _after_ the insert statement was executed, true if it is incremented before or during the execution
    • setCurrentValue

      public abstract void setCurrentValue(long currentValue)
      Sets a new start value.
      Parameters:
      currentValue - the current value - most likely as extracted from the target database