Package org.fastnate.generator.context
Class GeneratorContext
java.lang.Object
org.fastnate.generator.context.GeneratorContext
Represents the configuration and state for one or more
EntitySqlGenerators.- Author:
- Tobias Liefke
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate List<ContextModelListener> All listeners of this context.private static final Map<String, Class<? extends GeneratorDialect>> private Map<String, SequenceIdGenerator> The default sequence generator, if none is explicitly specified in aGeneratedValue.private Map<String, TableIdGenerator> The default table generator, if none is explicitly specified in aGeneratedValue.private final Map<Class<?>, EntityClass<?>> Contains the extracted metadata to every known class of anEntity.private final Map<String, EntityClass<?>> The mapping from thename of an entityto theextracted metadata.private GeneratorDialectIdentifies the SQL dialect for generating SQL statements.static final StringThe settings key for the target SQL dialect.private final Map<GeneratorContext.GeneratorId, IdGenerator> Mapping from the name of a generator to the generator itself.private static final org.slf4j.Loggerprivate intThe maximum count of columns that are used when referencing an entity using it's unique properties.static final StringThe settings key forwriteNullValues.static final StringThe settings key for the path to the persistence.xml, either relative to the current directory or absolute.static final StringThe settings key for the name of the persistence unit in the persistence.xml.static final StringThe settings key forpreferSequenceCurentValue.private booleanIndiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking forunique properties.private JpaProviderIdentifies the JPA provider to indicate implementation specific details.static final StringThe settings key for the JPA provider.static final StringThe settings key forquoteAllIdentifiers.private booleanIndicates to quote all identifiers.static final StringThe settings key forwriteRelativeIds.private final PropertiesContains the settings that were given during creation, resp. as read from the persistence configuration.private final Map<String, Map<Object, GenerationState>> Contains the state of single entities, maps from an entity name to the mapping of an id to its state.private final Map<String, GeneratorTable> Mapping from the names of all known database table to their description (including column information).static final StringThe settings key for themaxUniqueProperties.static final StringThe settings key for theuniquePropertyQuality.private UniquePropertyQualityIndicates what kind of properties are used for referencing an entity with its unique properties.private booleanIndicates to include null values in statements.private booleanIndicates that we write into a schema that is not empty. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a default generator context.GeneratorContext(Properties settings) Creates a new instance ofGeneratorContext.GeneratorContext(GeneratorDialect dialect) Creates a generator context for a dialect. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddContextModelListener(ContextModelListener listener) Adds a new listener to this context.private <K,T> T addContextObject(Map<K, ? super T> objects, BiConsumer<ContextModelListener, ? super T> listenerFunction, K key, T object) adjustIdentifier(String identifier) Quotes an object in the current database dialect, if it is marked for quoting with surrounding '"' or '`' in the column definition or if wequote all identifiers.buildQualifiedName(String catalog, String schema, String objectName) Builds the fully qualified name for the given database object.private static JpaProviderprivate voidprivate static Class<? extends GeneratorDialect> detectDialect(String setting) protected <T> voidfireContextObjectAdded(BiConsumer<ContextModelListener, T> listenerFunction, T contextObject) Fires an event to alllisteners.All listeners of this context.private IdGeneratorThe default sequence generator, if none is explicitly specified in aGeneratedValue.private IdGeneratorThe default table generator, if none is explicitly specified in aGeneratedValue.<E> EntityClass<E> getDescription(E entity) Finds the description for the class of an entity.<E> EntityClass<E> getDescription(Class<E> entityClass) Finds the description for a class.Map<Class<?>, EntityClass<?>> Contains the extracted metadata to every known class of anEntity.Map<String, EntityClass<?>> The mapping from thename of an entityto theextracted metadata.Identifies the SQL dialect for generating SQL statements.getGenerator(jakarta.persistence.GeneratedValue generatedValue, GeneratorTable table, GeneratorColumn column) Finds the correct generator for the given annotation.intThe maximum count of columns that are used when referencing an entity using it's unique properties.Identifies the JPA provider to indicate implementation specific details.Contains the settings that were given during creation, resp. as read from the persistence configuration.Contains the state of single entities, maps from an entity name to the mapping of an id to its state.(package private) Map<Object, GenerationState> getStates(EntityClass<?> entityClass) The entity states for the given entity class.Mapping from the names of all known database table to their description (including column information).Indicates what kind of properties are used for referencing an entity with its unique properties.booleanIndiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking forunique properties.booleanIndicates to quote all identifiers.booleanIndicates to include null values in statements.booleanIndicates that we write into a schema that is not empty.private static voidreadPersistenceFile(Properties settings) Tries to read any persistence file defined in the settings.voidregisterGenerators(AnnotatedElement element, GeneratorTable table) Registers theTableGeneratorandSequenceGeneratordeclared at the given element.voidRemoves a listener from this context.<A extends Annotation>
GeneratorTableresolveTable(jakarta.persistence.AssociationOverride override, A annotation, Function<A, String> catalogName, Function<A, String> schemaName, Function<A, String> tableName, String defaultTableName) Finds resp. builds the metadata to the given table from the given (optional) annotation.resolveTable(String catalogName, String schemaName, String tableName) Finds resp. builds the metadata to the given table.voidsetContextModelListeners(List<ContextModelListener> contextModelListeners) All listeners of this context.voidsetDefaultSequenceGenerators(Map<String, SequenceIdGenerator> defaultSequenceGenerators) The default sequence generator, if none is explicitly specified in aGeneratedValue.voidsetDefaultTableGenerators(Map<String, TableIdGenerator> defaultTableGenerators) The default table generator, if none is explicitly specified in aGeneratedValue.voidsetDialect(GeneratorDialect dialect) Identifies the SQL dialect for generating SQL statements.voidsetMaxUniqueProperties(int maxUniqueProperties) The maximum count of columns that are used when referencing an entity using it's unique properties.voidsetPreferSequenceCurentValue(boolean preferSequenceCurentValue) Indiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking forunique properties.voidsetProvider(JpaProvider provider) Identifies the JPA provider to indicate implementation specific details.voidsetQuoteAllIdentifiers(boolean quoteAllIdentifiers) Indicates to quote all identifiers.voidsetUniquePropertyQuality(UniquePropertyQuality uniquePropertyQuality) Indicates what kind of properties are used for referencing an entity with its unique properties.voidsetWriteNullValues(boolean writeNullValues) Indicates to include null values in statements.voidsetWriteRelativeIds(boolean writeRelativeIds) Indicates that we write into a schema that is not empty.voidBuilds all statements that are necessary to align ID generators in the database with the current IDs.
-
Field Details
-
log
private static final org.slf4j.Logger log -
PROVIDER_KEY
The settings key for the JPA provider. Contains the fully qualified class name of an extension ofJpaProvider. If none is given, the available from META-INF/services/org.fastnate.generator.provider.JpaProvider is used.- See Also:
-
PERSISTENCE_FILE_KEY
The settings key for the path to the persistence.xml, either relative to the current directory or absolute.- See Also:
-
PERSISTENCE_UNIT_KEY
The settings key for the name of the persistence unit in the persistence.xml. The first persistence unit is used, if none is explicitly set.- See Also:
-
DIALECT_KEY
The settings key for the target SQL dialect.Contains either the fully qualified name of a class that extends
GeneratorDialector the simple class name of one of the classes fromorg.fastnate.generator.dialect. The suffix 'Dialect' may be omitted in that case. For example 'MySql' would map toorg.fastnate.generator.dialect.MySqlDialect.If no dialect is set explicitly then the configured
persistence.xmlis scanned for a connection URL or provider specific dialect, which would be converted to our known dialects.If nothing is found, H2 is used as default.
- See Also:
-
NULL_VALUES_KEY
The settings key forwriteNullValues.- See Also:
-
RELATIVE_IDS_KEY
The settings key forwriteRelativeIds.- See Also:
-
QUOTE_ALL_IDENTIFIERS_KEY
The settings key forquoteAllIdentifiers.- See Also:
-
UNIQUE_PROPERTIES_QUALITY_KEY
The settings key for theuniquePropertyQuality.- See Also:
-
UNIQUE_PROPERTIES_MAX_KEY
The settings key for themaxUniqueProperties.- See Also:
-
PREFER_SEQUENCE_CURRENT_VALUE
The settings key forpreferSequenceCurentValue.- See Also:
-
DEFAULT_DIALECTS
-
dialect
Identifies the SQL dialect for generating SQL statements. Encapsulates the database specifica. -
provider
Identifies the JPA provider to indicate implementation specific details. -
maxUniqueProperties
private int maxUniquePropertiesThe maximum count of columns that are used when referencing an entity using it's unique properties. -
uniquePropertyQuality
Indicates what kind of properties are used for referencing an entity with its unique properties. -
preferSequenceCurentValue
private boolean preferSequenceCurentValueIndiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking forunique properties. -
writeRelativeIds
private boolean writeRelativeIdsIndicates that we write into a schema that is not empty. By default we write all IDs as absolute values and change the sequences / table generators at the end. But this would crash if there is data in the database already that uses the same IDs. So in the case of incremental updates, one should set this setting totrue- which will generate relative IDs which respect the existing IDs. -
writeNullValues
private boolean writeNullValuesIndicates to include null values in statements. -
quoteAllIdentifiers
private boolean quoteAllIdentifiersIndicates to quote all identifiers. Otherwise only those identifiers are quoted, which are surrounded by '"' or '`'. -
settings
Contains the settings that were given during creation, resp. as read from the persistence configuration. -
descriptions
Contains the extracted metadata to every known class of anEntity. -
descriptionsByName
The mapping from thename of an entityto theextracted metadata. -
tables
Mapping from the names of all known database table to their description (including column information). -
states
Contains the state of single entities, maps from an entity name to the mapping of an id to its state. -
generators
Mapping from the name of a generator to the generator itself. -
defaultSequenceGenerators
The default sequence generator, if none is explicitly specified in aGeneratedValue. -
defaultTableGenerators
The default table generator, if none is explicitly specified in aGeneratedValue. -
contextModelListeners
All listeners of this context.
-
-
Constructor Details
-
GeneratorContext
public GeneratorContext()Creates a default generator context. -
GeneratorContext
Creates a generator context for a dialect.- Parameters:
dialect- the database dialect to use during generation
-
GeneratorContext
Creates a new instance ofGeneratorContext.- Parameters:
settings- contains the settings
-
-
Method Details
-
createDefaultProvider
-
detectDialect
-
readPersistenceFile
Tries to read any persistence file defined in the settings.- Parameters:
settings- the current settings
-
addContextModelListener
Adds a new listener to this context.- Parameters:
listener- the listener that is interested in new discovered model elements
-
addContextObject
private <K,T> T addContextObject(Map<K, ? super T> objects, BiConsumer<ContextModelListener, ? super T> listenerFunction, K key, T object) -
adjustIdentifier
Quotes an object in the current database dialect, if it is marked for quoting with surrounding '"' or '`' in the column definition or if wequote all identifiers.- Parameters:
identifier- the name of the object (column, table, ...) according to the annotation- Returns:
- the quoted object name
-
buildQualifiedName
Builds the fully qualified name for the given database object.- Parameters:
catalog- the optional catalog nameschema- the optional schema nameobjectName- the name of the database object- Returns:
- the qualified name, as used by this dialect
-
detectDialect
private void detectDialect() -
fireContextObjectAdded
protected <T> void fireContextObjectAdded(BiConsumer<ContextModelListener, T> listenerFunction, T contextObject) Fires an event to alllisteners.- Parameters:
listenerFunction- the function that is called on the listenerscontextObject- the object to offer to the listener function
-
getDefaultSequenceGenerator
-
getDefaultTableGenerator
-
getDescription
Finds the description for a class.- Parameters:
entityClass- the class to lookup- Returns:
- the description for the class or
nullif the class is not anEntity
-
getDescription
Finds the description for the class of an entity.- Parameters:
entity- the entity to lookup- Returns:
- the description for the class of the entity
- Throws:
IllegalArgumentException- if the given object is noEntity
-
getGenerator
public IdGenerator getGenerator(jakarta.persistence.GeneratedValue generatedValue, GeneratorTable table, GeneratorColumn column) Finds the correct generator for the given annotation.- Parameters:
generatedValue- the annotation of the current primary keytable- the name of the current tablecolumn- the name of the current column- Returns:
- the generator that is responsible for managing the values
-
getStates
The entity states for the given entity class.- Parameters:
entityClass- the current entity class- Returns:
- the states of the entities of that class (with their IDs as keys)
-
registerGenerators
Registers theTableGeneratorandSequenceGeneratordeclared at the given element. If neither annotation is present, nothing happens.- Parameters:
element- the inspected class, method or fieldtable- the table of the current entity
-
removeContextModelListener
Removes a listener from this context.- Parameters:
listener- the listener that is not interested anymore
-
resolveTable
public <A extends Annotation> GeneratorTable resolveTable(jakarta.persistence.AssociationOverride override, A annotation, Function<A, String> catalogName, Function<A, String> schemaName, Function<A, String> tableName, String defaultTableName) Finds resp. builds the metadata to the given table from the given (optional) annotation.- Parameters:
override- contains the overrides for the mapping tableannotation- the optional annotation that contains any metadata to the tablecatalogName- finds the optional name of the catalog that contains the tableschemaName- finds the optional name of the schema that contains the tabletableName- finds the name of the tabledefaultTableName- the name of the talbe, if the annotation isnullor contains no value for the table name- Returns:
- the metadata for the given table
-
resolveTable
Finds resp. builds the metadata to the given table.- Parameters:
catalogName- the optional name of the catalog that contains the tableschemaName- the optional name of the schema that contains the tabletableName- the name of the table from the database- Returns:
- the metadata for the given table
-
writeAlignmentStatements
Builds all statements that are necessary to align ID generators in the database with the current IDs.- Parameters:
writer- the target of any write operation- Throws:
IOException- if the writer throws one
-
getDialect
Identifies the SQL dialect for generating SQL statements. Encapsulates the database specifica. -
getProvider
Identifies the JPA provider to indicate implementation specific details. -
getMaxUniqueProperties
public int getMaxUniqueProperties()The maximum count of columns that are used when referencing an entity using it's unique properties. -
getUniquePropertyQuality
Indicates what kind of properties are used for referencing an entity with its unique properties. -
isPreferSequenceCurentValue
public boolean isPreferSequenceCurentValue()Indiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking forunique properties. -
isWriteRelativeIds
public boolean isWriteRelativeIds()Indicates that we write into a schema that is not empty. By default we write all IDs as absolute values and change the sequences / table generators at the end. But this would crash if there is data in the database already that uses the same IDs. So in the case of incremental updates, one should set this setting totrue- which will generate relative IDs which respect the existing IDs. -
isWriteNullValues
public boolean isWriteNullValues()Indicates to include null values in statements. -
isQuoteAllIdentifiers
public boolean isQuoteAllIdentifiers()Indicates to quote all identifiers. Otherwise only those identifiers are quoted, which are surrounded by '"' or '`'. -
getSettings
Contains the settings that were given during creation, resp. as read from the persistence configuration. -
getDescriptions
Contains the extracted metadata to every known class of anEntity. -
getDescriptionsByName
The mapping from thename of an entityto theextracted metadata. -
getTables
Mapping from the names of all known database table to their description (including column information). -
getStates
Contains the state of single entities, maps from an entity name to the mapping of an id to its state. -
getDefaultSequenceGenerators
The default sequence generator, if none is explicitly specified in aGeneratedValue. -
getDefaultTableGenerators
The default table generator, if none is explicitly specified in aGeneratedValue. -
getContextModelListeners
All listeners of this context. -
setDialect
Identifies the SQL dialect for generating SQL statements. Encapsulates the database specifica. -
setProvider
Identifies the JPA provider to indicate implementation specific details. -
setMaxUniqueProperties
public void setMaxUniqueProperties(int maxUniqueProperties) The maximum count of columns that are used when referencing an entity using it's unique properties. -
setUniquePropertyQuality
Indicates what kind of properties are used for referencing an entity with its unique properties. -
setPreferSequenceCurentValue
public void setPreferSequenceCurentValue(boolean preferSequenceCurentValue) Indiciates to use "currval" of a sequence if the referenced entity is the last created entity for that sequence before checking forunique properties. -
setWriteRelativeIds
public void setWriteRelativeIds(boolean writeRelativeIds) Indicates that we write into a schema that is not empty. By default we write all IDs as absolute values and change the sequences / table generators at the end. But this would crash if there is data in the database already that uses the same IDs. So in the case of incremental updates, one should set this setting totrue- which will generate relative IDs which respect the existing IDs. -
setWriteNullValues
public void setWriteNullValues(boolean writeNullValues) Indicates to include null values in statements. -
setQuoteAllIdentifiers
public void setQuoteAllIdentifiers(boolean quoteAllIdentifiers) Indicates to quote all identifiers. Otherwise only those identifiers are quoted, which are surrounded by '"' or '`'. -
setDefaultSequenceGenerators
The default sequence generator, if none is explicitly specified in aGeneratedValue. -
setDefaultTableGenerators
The default table generator, if none is explicitly specified in aGeneratedValue. -
setContextModelListeners
All listeners of this context.
-