Class SequenceIdGenerator

java.lang.Object
org.fastnate.generator.context.IdGenerator
org.fastnate.generator.context.SequenceIdGenerator

public class SequenceIdGenerator extends IdGenerator
Stores the current value for a SequenceGenerator.
Author:
Tobias Liefke
  • Field Details

    • dialect

      private final GeneratorDialect dialect
      The current database dialect.
    • catalog

      private final String catalog
      The (optional) catalog that contains the schema of the sequence.
    • schema

      private final String schema
      The (optional) schema that contains the sequence.
    • sequenceName

      private final String sequenceName
      The name of the sequence.
    • qualifiedName

      private final String qualifiedName
      The fully qualfied name of the sequence, including the optional catalog and schema name.
    • relativeIds

      private final boolean relativeIds
      Indicates that the sequence is used when referencing existing IDs, instead of absolute IDs.
    • allocationSize

      private final int allocationSize
      The amount to increment by when allocating sequence numbers from the sequence.
    • initialValue

      private long initialValue
      The value from which the sequence object is to start generating.
    • nextValue

      private long nextValue
    • currentSequenceValue

      private long currentSequenceValue
  • Constructor Details

    • SequenceIdGenerator

      public SequenceIdGenerator(jakarta.persistence.SequenceGenerator generator, GeneratorContext context)
      Creates a new instance of SequenceIdGenerator.
      Parameters:
      generator - the annotation that contains our settings
      context - the current context
  • Method Details

    • addNextValue

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

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

      public long createNextValue()
      Description copied from class: IdGenerator
      Resolves the next value of this generator.
      Specified by:
      createNextValue in class IdGenerator
      Returns:
      the generated value
    • createPreInsertStatements

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

      public long getCurrentValue()
      Description copied from class: IdGenerator
      The last value returned by IdGenerator.createNextValue(Class).
      Specified by:
      getCurrentValue in class IdGenerator
      Returns:
      the current value
    • getExpression

      public ColumnExpression getExpression(GeneratorTable entityTable, GeneratorColumn column, Number targetId, boolean whereExpression)
      Description copied from class: IdGenerator
      Builds the reference to another entity that has the given ID.
      Specified by:
      getExpression in class IdGenerator
      Parameters:
      entityTable - the main table of the entity
      column - the name of the column of the ID
      targetId - 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 boolean isPostIncrement()
      Description copied from class: IdGenerator
      Indicates that IdGenerator.createNextValue(Class) should be called after the entity was written - as the value is not available before.
      Specified by:
      isPostIncrement in class IdGenerator
      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 void setCurrentValue(long currentValue)
      Description copied from class: IdGenerator
      Sets a new start value.
      Specified by:
      setCurrentValue in class IdGenerator
      Parameters:
      currentValue - the current value - most likely as extracted from the target database
    • getDialect

      public GeneratorDialect getDialect()
      The current database dialect.
    • getCatalog

      public String getCatalog()
      The (optional) catalog that contains the schema of the sequence.
    • getSchema

      public String getSchema()
      The (optional) schema that contains the sequence.
    • getSequenceName

      public String getSequenceName()
      The name of the sequence.
    • getQualifiedName

      public String getQualifiedName()
      The fully qualfied name of the sequence, including the optional catalog and schema name.
    • isRelativeIds

      public boolean isRelativeIds()
      Indicates that the sequence is used when referencing existing IDs, instead of absolute IDs.
    • getAllocationSize

      public int getAllocationSize()
      The amount to increment by when allocating sequence numbers from the sequence.
    • getInitialValue

      public long getInitialValue()
      The value from which the sequence object is to start generating.