Class BibernateFirstLevelCacheSession
java.lang.Object
io.github.blyznytsiaorg.bibernate.session.BibernateFirstLevelCacheSession
- All Implemented Interfaces:
BibernateSession
,Closeable
,AutoCloseable
BibernateFirstLevelCacheSession is an implementation of the BibernateSession interface
that introduces a first-level cache to improve the performance of entity retrieval operations.
It wraps an existing BibernateSession and maintains a first-level cache along with snapshots
for dirty checking. This class intercepts various session operations, manages the cache,
and delegates the actual database interactions to the underlying BibernateSession.
The first-level cache stores entities in-memory, allowing for quick retrieval without repeated database queries. Snapshots are used for dirty checking, identifying changes in entities and triggering necessary updates to the database.
This class is part of the Bibernate framework and supports common CRUD operations, query executions, and transaction management.
- Since:
- 1.0
- Author:
- Blyzhnytsia Team
-
Field Summary
Modifier and TypeFieldDescriptionprivate final ActionQueue
private final BibernateSession
private final Map
<EntityKey<?>, List<ColumnSnapshot>> -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprivate void
addToQueueOrExecute
(Runnable addToQueue, Runnable execute) private List
<ColumnSnapshot> buildEntitySnapshot
(Object entity) private void
void
close()
Closes the session, releasing any resources associated with it.void
Commits the current transaction.<T> void
Deletes an entity from the data store.<T> void
deleteAll
(Class<T> entityClass, Collection<T> entities) Deletes all records associated with the provided entities from the specified table.<T> void
deleteAllById
(Class<T> entityClass, Collection<Object> primaryKeys) Deletes records from the specified table based on the provided collection of primary key values.<T> List
<T> deleteByColumnValue
(Class<T> entityClass, String columnName, Object columnValue) Deletes records from the specified table where the value in the specified column matches the given criteria.<T> void
deleteById
(Class<T> entityClass, Object primaryKey) Deletes an entity from the data store by its primary key.int
Finds the number of records returned by a custom query.<T> List
<T> Retrieves all entities of a specified type from the data store.<T> List
<T> findAllByColumnValue
(Class<T> entityClass, String columnName, Object columnValue) Finds all entities of a given class with a specified column value.<T> List
<T> findAllById
(Class<T> entityClass, Collection<Object> primaryKeys) Retrieves entities based on a collection of primary keys.<T> Optional
<T> Finds an entity by its primary key.<T> List
<T> findByJoinTableField
(Class<T> entityClass, Field field, Object... bindValues) Finds entities of a given class based on a join table field.<T> List
<T> findByQuery
(Class<T> entityClass, String query, Object[] bindValues) Finds entities of a given class based on a custom query.<T> List
<T> findByWhere
(Class<T> entityClass, String whereQuery, Object[] bindValues) Finds entities of a given class based on a custom WHERE clause.<T> Optional
<T> findByWhereJoin
(Class<T> entityClass, Object[] bindValues) void
flush()
Flushes changes to the underlying database.getDao()
Gets the Data Access Object (DAO) associated with this session.private void
private <T> T
persistentContext
(Class<?> entityClass, T entityFromDb, EntityKey<?> entityKey, Object finalPrimaryKey) private <T> void
persistentContext
(Class<T> entityClass, List<T> entities) private <T> EntityAction
prepareDeleteByIdEntityAction
(Class<T> entityClass, Object primaryKey, EntityKey<T> entityKey) private <T> EntityKey
<T> prepareEntityKey
(Class<T> entityClass, Object primaryKey) private void
removeCacheAndSnapshotBy
(EntityKey<?> entityKey) private <T> void
removeCacheAndSnapshotBy
(Collection<EntityKey<T>> entityKeys, Class<T> entityClass, Collection<Object> primaryKeys) void
Rolls back the current transaction.<T> T
Saves an entity to the data store.<T> void
saveAll
(Class<T> entityClass, Collection<T> entities) Saves a collection of entities into the specified table.void
Starts a new transaction.<T> void
Updates an entity in the data store.private <T> void
update
(Class<T> entityClass, Object entity, List<ColumnSnapshot> diff)
-
Field Details
-
bibernateSession
-
actionQueue
-
firstLevelCache
-
snapshots
-
-
Constructor Details
-
BibernateFirstLevelCacheSession
public BibernateFirstLevelCacheSession()
-
-
Method Details
-
findById
Description copied from interface:BibernateSession
Finds an entity by its primary key.- Specified by:
findById
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entityprimaryKey
- the primary key of the entity- Returns:
- an Optional containing the found entity, or empty if not found
-
findAll
Description copied from interface:BibernateSession
Retrieves all entities of a specified type from the data store.- Specified by:
findAll
in interfaceBibernateSession
- Type Parameters:
T
- The generic type representing the entity class.- Parameters:
entityClass
- The class of the entity.- Returns:
- A list of all entities of the specified type in the data store.
-
findAllById
Description copied from interface:BibernateSession
Retrieves entities based on a collection of primary keys.- Specified by:
findAllById
in interfaceBibernateSession
- Type Parameters:
T
- The generic type representing the entity class.- Parameters:
entityClass
- The class of the entity.primaryKeys
- A collection of primary keys for identifying and retrieving specific records.- Returns:
- A list of entities matching the provided collection of primary keys.
-
findAllByColumnValue
public <T> List<T> findAllByColumnValue(Class<T> entityClass, String columnName, Object columnValue) Description copied from interface:BibernateSession
Finds all entities of a given class with a specified column value.- Specified by:
findAllByColumnValue
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entitycolumnName
- the name of the column to matchcolumnValue
- the value to match- Returns:
- a list of entities matching the criteria
-
findByWhere
Description copied from interface:BibernateSession
Finds entities of a given class based on a custom WHERE clause.- Specified by:
findByWhere
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entitywhereQuery
- the WHERE clause to applybindValues
- values to bind to the query parameters- Returns:
- a list of entities matching the criteria
-
findByJoinTableField
Description copied from interface:BibernateSession
Finds entities of a given class based on a join table field.- Specified by:
findByJoinTableField
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entityfield
- the field to join onbindValues
- values to bind to the query parameters- Returns:
- a list of entities matching the criteria
-
findByWhereJoin
- Specified by:
findByWhereJoin
in interfaceBibernateSession
-
findByQuery
Description copied from interface:BibernateSession
Finds entities of a given class based on a custom query.- Specified by:
findByQuery
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entityquery
- the custom query to executebindValues
- values to bind to the query parameters- Returns:
- a list of entities matching the criteria
-
update
Description copied from interface:BibernateSession
Updates an entity in the data store.- Specified by:
update
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entityentity
- the entity to update
-
find
Description copied from interface:BibernateSession
Finds the number of records returned by a custom query.- Specified by:
find
in interfaceBibernateSession
- Parameters:
query
- the custom query to executebindValues
- values to bind to the query parameters- Returns:
- the number of records returned by the query
-
save
Description copied from interface:BibernateSession
Saves an entity to the data store.- Specified by:
save
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entityentity
- the entity to save- Returns:
- the saved entity
-
saveAll
Description copied from interface:BibernateSession
Saves a collection of entities into the specified table.- Specified by:
saveAll
in interfaceBibernateSession
- Type Parameters:
T
- The generic type representing the entity class.- Parameters:
entityClass
- The Class object representing the type of entities to be saved.entities
- A collection of entities to be persisted into the database.
-
deleteById
Description copied from interface:BibernateSession
Deletes an entity from the data store by its primary key.- Specified by:
deleteById
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entityprimaryKey
- the primary key of the entity to delete
-
deleteAllById
Description copied from interface:BibernateSession
Deletes records from the specified table based on the provided collection of primary key values.- Specified by:
deleteAllById
in interfaceBibernateSession
- Type Parameters:
T
- The generic type representing the entity class.- Parameters:
entityClass
- The Class object representing the type of entities for which records will be deleted.primaryKeys
- A collection of primary key values identifying the records to be deleted.
-
deleteByColumnValue
Description copied from interface:BibernateSession
Deletes records from the specified table where the value in the specified column matches the given criteria.- Specified by:
deleteByColumnValue
in interfaceBibernateSession
- Type Parameters:
T
- The generic type representing the entity class.- Parameters:
entityClass
- The Class object representing the type of entities for which records will be deleted.columnName
- The name of the column used in the WHERE condition for deletion.columnValue
- The value to match in the specified column for deletion.- Returns:
- A list of entities of type T that were deleted from the table.
-
delete
Description copied from interface:BibernateSession
Deletes an entity from the data store.- Specified by:
delete
in interfaceBibernateSession
- Type Parameters:
T
- the type of the entity- Parameters:
entityClass
- the class of the entityentity
- the entity to delete
-
deleteAll
Description copied from interface:BibernateSession
Deletes all records associated with the provided entities from the specified table.- Specified by:
deleteAll
in interfaceBibernateSession
- Type Parameters:
T
- The generic type representing the entity class.- Parameters:
entityClass
- The Class object representing the type of entities to be deleted.entities
- A collection of entities whose corresponding records will be deleted.
-
flush
public void flush()Description copied from interface:BibernateSession
Flushes changes to the underlying database. This method synchronizes the state of the Bibernate session with the database. Any changes that have been queued for insertion, update, or deletion are executed immediately.- Specified by:
flush
in interfaceBibernateSession
-
close
public void close()Description copied from interface:BibernateSession
Closes the session, releasing any resources associated with it.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBibernateSession
- Specified by:
close
in interfaceCloseable
-
getDao
Description copied from interface:BibernateSession
Gets the Data Access Object (DAO) associated with this session.- Specified by:
getDao
in interfaceBibernateSession
- Returns:
- the DAO instance
-
startTransaction
Description copied from interface:BibernateSession
Starts a new transaction. This method should be called before any operations that are meant to be part of a transaction.- Specified by:
startTransaction
in interfaceBibernateSession
- Throws:
SQLException
- if a database access error occurs or this method is called on a closed connection
-
commitTransaction
Description copied from interface:BibernateSession
Commits the current transaction. This method should be called to make all changes made within the transaction permanent.- Specified by:
commitTransaction
in interfaceBibernateSession
- Throws:
SQLException
- if a database access error occurs, the connection is closed or this method is called when no transaction is active
-
rollbackTransaction
Description copied from interface:BibernateSession
Rolls back the current transaction. This method should be called if any errors occur within the transaction and the changes made need to be discarded.- Specified by:
rollbackTransaction
in interfaceBibernateSession
- Throws:
SQLException
- if a database access error occurs, the connection is closed or this method is called when no transaction is active
-
buildEntitySnapshot
-
update
-
performDirtyChecking
private void performDirtyChecking() -
prepareDeleteByIdEntityAction
private <T> EntityAction prepareDeleteByIdEntityAction(Class<T> entityClass, Object primaryKey, EntityKey<T> entityKey) -
persistentContext
-
persistentContext
-
clearCacheAndSnapshots
private void clearCacheAndSnapshots() -
removeCacheAndSnapshotBy
-
removeCacheAndSnapshotBy
private <T> void removeCacheAndSnapshotBy(Collection<EntityKey<T>> entityKeys, Class<T> entityClass, Collection<Object> primaryKeys) -
prepareEntityKey
-
addToQueueOrExecute
-