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
FieldsModifier and TypeFieldDescriptionprivate final BibernateDatabaseSettingsThe settings related to the Bibernate database.private final EntityPersistentManages the persistence state of entities during database operations.List of executed SQL queries during the session.private final IdentityThe identity manager responsible for generating unique identifiers for entities.private final SqlBuilderThe SQL builder used to construct SQL queries. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddToExecutedQueries(String query) voidCommits the current transaction.private <T> StringcreateLeftJoinQuery(Class<T> entityClass) <T> voidDeletes an entity of typeTby providing the entity instance.<T> voiddeleteAll(Class<T> entityClass, Collection<T> entities) Deletes all entities of typeTfrom the database based on the provided collection of entities.<T> voiddeleteAllById(Class<T> entityClass, Collection<Object> primaryKeys) Deletes entities of typeTby their primary keys.<T> List<T> deleteByColumnValue(Class<T> entityClass, String columnName, Object value) Deletes entities of typeTbased on a specified column value.private <T> List<T> deleteByColumnValue(Class<T> entityClass, String columnName, Object value, boolean returnDeletedEntities) <T> voiddeleteById(Class<T> entityClass, Object primaryKey) Deletes an entity of typeTby its primary key.intExecutes 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 typeTbased 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 typeTusing 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 typeTbased 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 typeTbased 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 typeTbased on the specified WHERE condition.<T> List<T> findByWhereJoin(Class<T> entityClass, Object... bindValues) Retrieves a list of entities of typeTusing a left join for fetching, based on the specified entity class and optional bind values.private Transactionprivate booleanhasAnyOneToOneEagerFetchType(EntityMetadata entityMetadata) private booleanprivate booleanisUpdateTimestamp(Field field) private voidpopulatePreparedStatement(Object[] bindValues, PreparedStatement statement) private voidpopulatePreparedStatement(Object entity, PreparedStatement statement, String fieldIdName, Object fieldIdValue, Number fieldVersionValue, List<ColumnSnapshot> diff) preparePrimaryKeyToVersionValues(Class<T> entityClass, Collection<T> entities, boolean isVersionFound) private <T> StringprepareQuery(Class<T> entityClass, boolean isVersionFound, String tableName, String fieldIdName, List<org.flywaydb.core.internal.util.Pair<Object, Object>> primaryKeyToVersionValues, List<Object> primaryKeys) private <T> voidremoveToManyRelations(Class<T> entityClass, Object value, BibernateSession session, List<EntityColumnDetails> relationsForRemoval) private <T> voidremoveToOneRelations(List<T> deletedEntities, BibernateSession session, List<EntityColumnDetails> relationsForRemoval) voidRolls back the current transaction.<T> TSaves an entity of typeTin the database.<T> voidsaveAll(Class<T> entityClass, Collection<T> entities) Saves a collection of entities of typeTin the database.private voidvoidStarts a new transaction and put it into ThreadLocal.private voidthrowErrorMessage(String errorMessage, Exception exe) <T> voidupdate(Class<T> entityClass, Object entity, List<ColumnSnapshot> diff) Updates an entity of typeTin 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, aNonUniqueResultExceptionis thrown.- Specified by:
findByIdin 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
Optionalcontaining the found entity, or an empty Optional if not found. - Throws:
NullPointerException- if eitherentityClassorprimaryKeyis 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:
findAllin 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- ifentityClassis null.BibernateGeneralException- if an error occurs while executing the query.
-
findAllById
Retrieves entities by their primary keys from the database.- Specified by:
findAllByIdin 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- ifentityClassorprimaryKeysis 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 typeTbased 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:
findAllByColumnValuein 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 typeTbased 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:
findByWherein 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 typeTbased 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:
findByJoinTableFieldin 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 typeTusing a left join for fetching, based on the specified entity class and optional bind values.- Specified by:
findByWhereJoinin 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 typeTusing a left join for fetching, based on the specified entity class, query, and optional bind values.- Specified by:
findAllByWhereJoinin 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 typeTbased 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:
findByQueryin 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 typeTin 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 typeTin 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 typeTin 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 typeTby its primary key.- Specified by:
deleteByIdin 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 typeTbased on a specified column value.- Specified by:
deleteByColumnValuein 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 typeTby their primary keys.- Specified by:
deleteAllByIdin 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 typeTby providing the entity instance. -
deleteAll
Deletes all entities of typeTfrom 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:
startTransactionin interfaceDao- Throws:
SQLException- If an SQL exception occurs while starting the transaction.
-
commitTransaction
Commits the current transaction.- Specified by:
commitTransactionin interfaceDao- Throws:
SQLException- If an SQL exception occurs while committing the transaction.
-
rollbackTransaction
Rolls back the current transaction.- Specified by:
rollbackTransactionin 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
-