Class AbstractGenerator
java.lang.Object
io.github.blyznytsiaorg.bibernate.dao.jdbc.identity.AbstractGenerator
- Direct Known Subclasses:
IdentityIdGenerator
,NoneIdGenerator
,SequenceIdGenerator
An abstract base class for identity generators in Bibernate, providing common functionality
which can be used for generating insert queries to database.
This class is designed to be extended by specific generators that handle the id generation and generations of insert queries
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractGenerator
(BibernateDatabaseSettings bibernateDatabaseSettings, List<String> executedQueries) Constructs an AbstractGenerator with the specified Bibernate database settings and a list to store executed queries. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addToExecutedQueries
(String query) Adds a query to the list of executed queries if query collection is enabled.protected void
addUpdatedEntity
(Object entity) Adds an updated entity to the current transaction to be able to clean up ids in case of rollback transaction.protected Integer
Retrieves the batch size from the Bibernate database settings.getGeneratedValue
(Field field, Field generatedValueField, Object generatedId) Retrieves the generated value for a field, handling the case where it matches the generated ID.protected <T> void
insertManyToManyJoinTable
(Collection<T> entities, DataSource dataSource) Generates and executes insert query for join table in case of many-to-many relation.protected void
populatePreparedStatement
(Object entity, PreparedStatement statement) Populates a prepared statement with values from the given entity.protected void
populatePreparedStatement
(Object entity, PreparedStatement statement, Object generatedId) Populates a prepared statement with values from the given entity, including the generated ID.protected void
populatePreparedStatement
(List<Object> values, PreparedStatement statement) Populates a prepared statement with a list of values.protected void
Conditionally logins a provided SQL query based on the 'showSql' setting.
-
Field Details
-
bibernateDatabaseSettings
-
executedQueries
-
-
Constructor Details
-
AbstractGenerator
protected AbstractGenerator(BibernateDatabaseSettings bibernateDatabaseSettings, List<String> executedQueries) Constructs an AbstractGenerator with the specified Bibernate database settings and a list to store executed queries.- Parameters:
bibernateDatabaseSettings
- The database settings for Bibernate.executedQueries
- The list to store executed queries.
-
-
Method Details
-
addToExecutedQueries
Adds a query to the list of executed queries if query collection is enabled.- Parameters:
query
- The SQL query to be added to the executed queries list.
-
showSql
Conditionally logins a provided SQL query based on the 'showSql' setting.- Parameters:
logSql
- The SQL logging action to be executed.
-
getBatchSize
Retrieves the batch size from the Bibernate database settings.- Returns:
- The batch size for database queries.
-
populatePreparedStatement
protected void populatePreparedStatement(Object entity, PreparedStatement statement, Object generatedId) throws SQLException Populates a prepared statement with values from the given entity, including the generated ID.- Parameters:
entity
- The entity for which the prepared statement is being populated.statement
- The prepared statement to be populated.generatedId
- The generated ID for the entity.- Throws:
SQLException
- If a SQL error occurs.
-
populatePreparedStatement
protected void populatePreparedStatement(Object entity, PreparedStatement statement) throws SQLException Populates a prepared statement with values from the given entity.- Parameters:
entity
- The entity for which the prepared statement is being populated.statement
- The prepared statement to be populated.- Throws:
SQLException
- If a SQL error occurs.
-
populatePreparedStatement
protected void populatePreparedStatement(List<Object> values, PreparedStatement statement) throws SQLException Populates a prepared statement with a list of values.- Parameters:
values
- The list of values to be used in the prepared statement.statement
- The prepared statement to be populated.- Throws:
SQLException
- If a SQL error occurs.
-
getGeneratedValue
protected static Optional<Object> getGeneratedValue(Field field, Field generatedValueField, Object generatedId) Retrieves the generated value for a field, handling the case where it matches the generated ID.- Parameters:
field
- The field for which the generated value is retrieved.generatedValueField
- The field representing the generated value.generatedId
- The generated ID.- Returns:
- An optional containing the generated value or empty if it is not the generated ID.
-
addUpdatedEntity
Adds an updated entity to the current transaction to be able to clean up ids in case of rollback transaction.- Parameters:
entity
- The entity to be added to the transaction.
-
insertManyToManyJoinTable
Generates and executes insert query for join table in case of many-to-many relation.- Type Parameters:
T
- The type of entities in the collection.- Parameters:
entities
- The collection of entities participating in the many-to-many relationship.dataSource
- The data source for obtaining a database connection.- Throws:
BibernateGeneralException
- If an error occurs while executing the SQL statements.
-