Class EntityDao
- All Implemented Interfaces:
Dao
This DAO provides methods for interacting with the database to perform CRUD operations on entities. It uses a combination of JDBC and custom SQL queries to execute operations such as saving, updating, deleting, and querying entities. The class is designed to be used with various entities and supports flexible querying.
The DAO utilizes the provided SqlBuilder
for constructing SQL queries,
BibernateDatabaseSettings
for obtaining database-related settings,
EntityPersistent
for entity-related persistence operations,
and Identity
for managing identity generators.
The class supports logging executed queries, which can be useful for debugging and monitoring database interactions.
Note: This class assumes that entities follow the convention of having an identifier (ID) column, and it supports primary key-based operations accordingly.
- Since:
- 1.0
- Author:
- Blyzhnytsia Team
-
Field Summary
Modifier and TypeFieldDescriptionprivate final BibernateDatabaseSettings
The settings related to the Bibernate database.private final EntityPersistent
Manages the persistence state of entities during database operations.List of executed SQL queries during the session.private final Identity
The identity manager responsible for generating unique identifiers for entities.private final SqlBuilder
The SQL builder used to construct SQL queries. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprivate void
addToExecutedQueries
(String query) void
Commits the current transaction.private <T> String
createLeftJoinQuery
(Class<T> entityClass) <T> void
Deletes an entity of typeT
by providing the entity instance.<T> void
deleteAll
(Class<T> entityClass, Collection<T> entities) Deletes all entities of typeT
from the database based on the provided collection of entities.<T> void
deleteAllById
(Class<T> entityClass, Collection<Object> primaryKeys) Deletes entities of typeT
by their primary keys.<T> List
<T> deleteByColumnValue
(Class<T> entityClass, String columnName, Object value) Deletes entities of typeT
based on a specified column value.private <T> List
<T> deleteByColumnValue
(Class<T> entityClass, String columnName, Object value, boolean returnDeletedEntities) <T> void
deleteById
(Class<T> entityClass, Object primaryKey) Deletes an entity of typeT
by its primary key.int
Executes a SQL query and retrieves an integer result.<T> List
<T> Retrieves all entities of a given class from the database.<T> List
<T> findAllByColumnValue
(Class<T> entityClass, String columnName, Object columnValue) Retrieves a list of entities of typeT
based on the specified column's value.<T> List
<T> findAllById
(Class<T> entityClass, Collection<Object> primaryKeys) Retrieves entities by their primary keys from the database.<T> List
<T> findAllByWhereJoin
(Class<T> entityClass, String query, Object... bindValues) Retrieves a list of entities of typeT
using a left join for fetching, based on the specified entity class, query, and optional bind values.<T> Optional
<T> Retrieves an entity by its primary key.<T> List
<T> findByJoinTableField
(Class<T> entityClass, Field field, Object... bindValues) Retrieves a list of entities of typeT
based on the specified field and optional bind values.<T> List
<T> findByQuery
(Class<T> entityClass, String query, Object... bindValues) Retrieves a list of entities of typeT
based on the provided SQL query and optional bind values.<T> List
<T> findByWhere
(Class<T> entityClass, String whereCondition, Object... bindValues) Retrieves a list of entities of typeT
based on the specified WHERE condition.<T> List
<T> findByWhereJoin
(Class<T> entityClass, Object... bindValues) Retrieves a list of entities of typeT
using a left join for fetching, based on the specified entity class and optional bind values.private Transaction
private boolean
hasAnyOneToOneEagerFetchType
(EntityMetadata entityMetadata) private boolean
private boolean
isUpdateTimestamp
(Field field) private void
populatePreparedStatement
(Object[] bindValues, PreparedStatement statement) private void
populatePreparedStatement
(Object entity, PreparedStatement statement, String fieldIdName, Object fieldIdValue, Number fieldVersionValue, List<ColumnSnapshot> diff) preparePrimaryKeyToVersionValues
(Class<T> entityClass, Collection<T> entities, boolean isVersionFound) private <T> String
prepareQuery
(Class<T> entityClass, boolean isVersionFound, String tableName, String fieldIdName, List<org.flywaydb.core.internal.util.Pair<Object, Object>> primaryKeyToVersionValues, List<Object> primaryKeys) private <T> void
removeToManyRelations
(Class<T> entityClass, Object value, BibernateSession session, List<EntityColumnDetails> relationsForRemoval) private <T> void
removeToOneRelations
(List<T> deletedEntities, BibernateSession session, List<EntityColumnDetails> relationsForRemoval) void
Rolls back the current transaction.<T> T
Saves an entity of typeT
in the database.<T> void
saveAll
(Class<T> entityClass, Collection<T> entities) Saves a collection of entities of typeT
in the database.private void
void
Starts a new transaction and put it into ThreadLocal.private void
throwErrorMessage
(String errorMessage, Exception exe) <T> void
update
(Class<T> entityClass, Object entity, List<ColumnSnapshot> diff) Updates an entity of typeT
in the database based on the provided differences.
-
Field Details
-
sqlBuilder
The SQL builder used to construct SQL queries. -
bibernateDatabaseSettings
The settings related to the Bibernate database. -
entityPersistent
Manages the persistence state of entities during database operations. -
identity
The identity manager responsible for generating unique identifiers for entities. -
executedQueries
List of executed SQL queries during the session. Provides a record of queries executed.
-
-
Constructor Details
-
EntityDao
public EntityDao()
-
-
Method Details
-
findById
Retrieves an entity by its primary key. If the result set contains more than one entity, aNonUniqueResultException
is thrown.- Specified by:
findById
in interfaceDao
- Type Parameters:
T
- The type of the entity.- Parameters:
entityClass
- The class of the entity.primaryKey
- The primary key value to search for.- Returns:
- An
Optional
containing the found entity, or an empty Optional if not found. - Throws:
NullPointerException
- if eitherentityClass
orprimaryKey
is null.NonUniqueResultException
- if more than one entity is found for the given primary key.
-
findAll
Retrieves all entities of a given class from the database.- Specified by:
findAll
in interfaceDao
- Type Parameters:
T
- The type of the entity.- Parameters:
entityClass
- The class of the entity.- Returns:
- A list containing all entities of the specified class.
- Throws:
NullPointerException
- ifentityClass
is null.BibernateGeneralException
- if an error occurs while executing the query.
-
findAllById
Retrieves entities by their primary keys from the database.- Specified by:
findAllById
in interfaceDao
- Type Parameters:
T
- The type of the entity.- Parameters:
entityClass
- The class of the entity.primaryKeys
- The collection of primary key values to search for.- Returns:
- A list containing the found entities.
- Throws:
NullPointerException
- ifentityClass
orprimaryKeys
is null.BibernateGeneralException
- if an error occurs while executing the query.
-
findAllByColumnValue
public <T> List<T> findAllByColumnValue(Class<T> entityClass, String columnName, Object columnValue) Retrieves a list of entities of typeT
based on the specified column's value.This method constructs a WHERE condition using the provided column name and value and delegates to
findByWhere(Class, String, Object...)
method for execution.- Specified by:
findAllByColumnValue
in interfaceDao
- Type Parameters:
T
- The type of entities to retrieve.- Parameters:
entityClass
- The class of the entity.columnName
- The name of the column.columnValue
- The value to match in the specified column.- Returns:
- A list of entities matching the specified column value.
-
findByWhere
Retrieves a list of entities of typeT
based on the specified WHERE condition.This method constructs a SELECT query using the provided entity class, WHERE condition, and optional bind values, and delegates to
findByQuery(Class, String, Object...)
method for execution.If the entity has any one-to-one eager fetch type relationships, it utilizes a left join for fetching.
- Specified by:
findByWhere
in interfaceDao
- Type Parameters:
T
- The type of entities to retrieve.- Parameters:
entityClass
- The class of the entity.whereCondition
- The WHERE condition for the query.bindValues
- The optional bind values for the WHERE condition.- Returns:
- A list of entities matching the specified WHERE condition.
-
findByJoinTableField
Retrieves a list of entities of typeT
based on the specified field and optional bind values.This method constructs a SELECT query using the provided entity class, field, and optional bind values, and delegates to
findByQuery(Class, String, Object...)
method for execution.If the entity has any one-to-one eager fetch type relationships, it utilizes a left join for fetching.
- Specified by:
findByJoinTableField
in interfaceDao
- Type Parameters:
T
- The type of entities to retrieve.- Parameters:
entityClass
- The class of the entity.field
- The field for the join operation.bindValues
- The optional bind values for the join operation.- Returns:
- A list of entities matching the specified join table field.
-
findByWhereJoin
Retrieves a list of entities of typeT
using a left join for fetching, based on the specified entity class and optional bind values.- Specified by:
findByWhereJoin
in interfaceDao
- Type Parameters:
T
- The type of entities to retrieve.- Parameters:
entityClass
- The class of the entity.bindValues
- The optional bind values for the left join operation.- Returns:
- A list of entities matching the specified left join operation.
-
findAllByWhereJoin
Retrieves a list of entities of typeT
using a left join for fetching, based on the specified entity class, query, and optional bind values.- Specified by:
findAllByWhereJoin
in interfaceDao
- Type Parameters:
T
- The type of entities to retrieve.- Parameters:
entityClass
- The class of the entity.query
- The query for the left join operation.bindValues
- The optional bind values for the left join operation.- Returns:
- A list of entities matching the specified left join operation.
-
findByQuery
Retrieves a list of entities of typeT
based on the provided SQL query and optional bind values.This method prepares a SQL query, executes it, and maps the result set to entities of type
T
.- Specified by:
findByQuery
in interfaceDao
- Type Parameters:
T
- The type of entities to retrieve.- Parameters:
entityClass
- The class of the entity.query
- The SQL query to execute.bindValues
- The optional bind values for the query.- Returns:
- A list of entities based on the specified SQL query.
-
find
Executes a SQL query and retrieves an integer result. -
update
Updates an entity of typeT
in the database based on the provided differences.This method prepares and executes an SQL update query and handles version checking if a version column is present.
-
save
Saves an entity of typeT
in the database.This method sets the version value if null, saves the entity using the identity, generates sql query for insert using different id generators.
-
saveAll
Saves a collection of entities of typeT
in the database.This method sets the version value if null, saves the collection using the identity, generates sql query for insert using different id generators.
-
deleteById
Deletes an entity of typeT
by its primary key.- Specified by:
deleteById
in interfaceDao
- Type Parameters:
T
- The type of entity to delete.- Parameters:
entityClass
- The class of the entity.primaryKey
- The primary key of the entity to delete.
-
deleteByColumnValue
Deletes entities of typeT
based on a specified column value.- Specified by:
deleteByColumnValue
in interfaceDao
- Type Parameters:
T
- The type of entities to delete.- Parameters:
entityClass
- The class of the entities.columnName
- The name of the column to use for deletion.value
- The value of the column to match for deletion.- Returns:
- A list of deleted entities.
-
deleteAllById
Deletes entities of typeT
by their primary keys.- Specified by:
deleteAllById
in interfaceDao
- Type Parameters:
T
- The type of entities to delete.- Parameters:
entityClass
- The class of the entities.primaryKeys
- The collection of primary keys for entities to delete.
-
delete
Deletes an entity of typeT
by providing the entity instance. -
deleteAll
Deletes all entities of typeT
from the database based on the provided collection of entities.This method takes a collection of entities, determines their primary keys and version values (if applicable), and executes batch delete queries to remove the entities from the database.
-
startTransaction
Starts a new transaction and put it into ThreadLocal. If transaction is already in ThreadLocal will be kept the same.- Specified by:
startTransaction
in interfaceDao
- Throws:
SQLException
- If an SQL exception occurs while starting the transaction.
-
commitTransaction
Commits the current transaction.- Specified by:
commitTransaction
in interfaceDao
- Throws:
SQLException
- If an SQL exception occurs while committing the transaction.
-
rollbackTransaction
Rolls back the current transaction.- Specified by:
rollbackTransaction
in interfaceDao
- Throws:
SQLException
- If an SQL exception occurs while rolling back the transaction.
-
createLeftJoinQuery
-
deleteByColumnValue
-
getTransaction
- Throws:
SQLException
-
removeToManyRelations
private <T> void removeToManyRelations(Class<T> entityClass, Object value, BibernateSession session, List<EntityColumnDetails> relationsForRemoval) -
removeToOneRelations
private <T> void removeToOneRelations(List<T> deletedEntities, BibernateSession session, List<EntityColumnDetails> relationsForRemoval) -
preparePrimaryKeyToVersionValues
private <T> List<org.flywaydb.core.internal.util.Pair<Object,Object>> preparePrimaryKeyToVersionValues(Class<T> entityClass, Collection<T> entities, boolean isVersionFound) -
prepareQuery
-
addToExecutedQueries
-
showSql
-
populatePreparedStatement
private void populatePreparedStatement(Object[] bindValues, PreparedStatement statement) throws SQLException - Throws:
SQLException
-
populatePreparedStatement
private void populatePreparedStatement(Object entity, PreparedStatement statement, String fieldIdName, Object fieldIdValue, Number fieldVersionValue, List<ColumnSnapshot> diff) throws SQLException - Throws:
SQLException
-
isIdField
-
isUpdateTimestamp
-
throwErrorMessage
-
hasAnyOneToOneEagerFetchType
-