Package org.fastnate.generator.context
Class TableIdGenerator
java.lang.Object
org.fastnate.generator.context.IdGenerator
org.fastnate.generator.context.TableIdGenerator
Saves the current value for a
TableGenerator.
The content of the value column is interpreted like nextValue of a sequence, which means the maximum allocated
value is allways at most value column value - allocationSize.- Author:
- Tobias Liefke
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate final GeneratorContextprivate final GeneratorTableprivate longprivate longThe maximum allocated value.private longprivate final GeneratorColumnprivate final ColumnExpressionprivate final booleanprivate final GeneratorColumn -
Constructor Summary
ConstructorsModifierConstructorDescriptionTableIdGenerator(String tableName, jakarta.persistence.TableGenerator generator, GeneratorContext context) Creates a new instance ofSequenceIdGenerator.privateTableIdGenerator(GeneratorContext context, boolean relativeIds, GeneratorTable generatorTable, GeneratorColumn pkColumn, ColumnExpression pkColumnValue, GeneratorColumn valueColumn, int allocationSize, long initialValue, long nextValue, long maxAllocatedValue) Creates a newTableIdGeneratorinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddNextValue(TableStatement statement, GeneratorColumn column, Number value) Adds the generated value to the given statement.voidalignNextValue(StatementsWriter writer) Creates all statements that are necessary to set the next value created from the database iscurrentValue + 1.longResolves the next value of this generator.voidCreates the statements that are needed in the output beforeIdGenerator.addNextValue(TableStatement, GeneratorColumn, Number).derive(GeneratorTable currentTable) Some implementations (like the Hibernate table generator) create a different generator, depending on the table name.intlongThe last value returned byIdGenerator.createNextValue(Class).getExpression(GeneratorTable table, GeneratorColumn column, Number targetId, boolean whereExpression) Builds the reference to another entity that has the given ID.longlongThe maximum allocated value.longprivate longbooleanIndicates thatIdGenerator.createNextValue(Class)should be called after the entity was written - as the value is not available before.booleanvoidsetCurrentValue(long currentValue) Sets a new start value.private voidwriteAllocatedValue(StatementsWriter writer, boolean firstUpdate, long allocatedValue) Methods inherited from class org.fastnate.generator.context.IdGenerator
createNextValue
-
Field Details
-
context
-
relativeIds
private final boolean relativeIds -
generatorTable
-
pkColumn
-
pkColumnValue
-
valueColumn
-
allocationSize
private final int allocationSize -
initialValue
private long initialValue -
nextValue
private long nextValue -
maxAllocatedValue
private long maxAllocatedValueThe maximum allocated value. The value column value in the table is at any time byallocationSizegreater.
-
-
Constructor Details
-
TableIdGenerator
public TableIdGenerator(String tableName, jakarta.persistence.TableGenerator generator, GeneratorContext context) Creates a new instance ofSequenceIdGenerator.- Parameters:
tableName- the name of the current tablegenerator- the annotation that contains our settingscontext- the current context
-
TableIdGenerator
private TableIdGenerator(GeneratorContext context, boolean relativeIds, GeneratorTable generatorTable, GeneratorColumn pkColumn, ColumnExpression pkColumnValue, GeneratorColumn valueColumn, int allocationSize, long initialValue, long nextValue, long maxAllocatedValue) Creates a newTableIdGeneratorinstance.- Parameters:
context-relativeIds-generatorTable-pkColumn-pkColumnValue-valueColumn-allocationSize-initialValue-nextValue-maxAllocatedValue- The maximum allocated value. The value column value in the table is at any time byallocationSizegreater.
-
-
Method Details
-
addNextValue
Description copied from class:IdGeneratorAdds 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:
addNextValuein classIdGenerator- Parameters:
statement- the current insert statementcolumn- the ID column of the current tablevalue- the current value of the column, previously generated withIdGenerator.createNextValue()
-
alignNextValue
Description copied from class:IdGeneratorCreates all statements that are necessary to set the next value created from the database iscurrentValue + 1.- Specified by:
alignNextValuein classIdGenerator- Parameters:
writer- the target of the created statements- Throws:
IOException- if the writer throws one
-
createNextValue
public long createNextValue()Description copied from class:IdGeneratorResolves the next value of this generator.- Specified by:
createNextValuein classIdGenerator- Returns:
- the generated value
-
createPreInsertStatements
Description copied from class:IdGeneratorCreates the statements that are needed in the output beforeIdGenerator.addNextValue(TableStatement, GeneratorColumn, Number).- Overrides:
createPreInsertStatementsin classIdGenerator- Parameters:
writer- target for the created statements- Throws:
IOException- if the writer throws one
-
derive
Description copied from class:IdGeneratorSome implementations (like the Hibernate table generator) create a different generator, depending on the table name.- Overrides:
derivein classIdGenerator- Parameters:
currentTable- the current entity table- Returns:
- the generator
-
getCurrentValue
public long getCurrentValue()Description copied from class:IdGeneratorThe last value returned byIdGenerator.createNextValue(Class).- Specified by:
getCurrentValuein classIdGenerator- Returns:
- the current value
-
getExpression
public ColumnExpression getExpression(GeneratorTable table, GeneratorColumn column, Number targetId, boolean whereExpression) Description copied from class:IdGeneratorBuilds the reference to another entity that has the given ID.- Specified by:
getExpressionin classIdGenerator- Parameters:
table- the main table of the entitycolumn- the name of the column of the IDtargetId- the current value of the IDwhereExpression- indicates if this expression is needed for a "SELECT ... WHERE ..." - some dialects behave differently in this situation- Returns:
- the expression for selecting the ID
-
getValueColumnValue
private long getValueColumnValue() -
isPostIncrement
public boolean isPostIncrement()Description copied from class:IdGeneratorIndicates thatIdGenerator.createNextValue(Class)should be called after the entity was written - as the value is not available before.- Specified by:
isPostIncrementin classIdGenerator- Returns:
trueif the database increments the value _after_ the insert statement was executed,trueif it is incremented before or during the execution
-
setCurrentValue
public void setCurrentValue(long currentValue) Description copied from class:IdGeneratorSets a new start value.- Specified by:
setCurrentValuein classIdGenerator- Parameters:
currentValue- the current value - most likely as extracted from the target database
-
writeAllocatedValue
private void writeAllocatedValue(StatementsWriter writer, boolean firstUpdate, long allocatedValue) throws IOException - Throws:
IOException
-
getContext
-
isRelativeIds
public boolean isRelativeIds() -
getGeneratorTable
-
getPkColumn
-
getPkColumnValue
-
getValueColumn
-
getAllocationSize
public int getAllocationSize() -
getInitialValue
public long getInitialValue() -
getNextValue
public long getNextValue() -
getMaxAllocatedValue
public long getMaxAllocatedValue()The maximum allocated value. The value column value in the table is at any time byallocationSizegreater.
-