Class BibernateSecondLevelCacheSession

java.lang.Object
io.github.blyznytsiaorg.bibernate.session.BibernateSecondLevelCacheSession
All Implemented Interfaces:
BibernateSession, Closeable, AutoCloseable

public class BibernateSecondLevelCacheSession extends Object implements BibernateSession
Implementation of the BibernateSession interface that provides second-level caching functionality for immutable entities fetched from the database. It utilizes a distributed set for caching entities and extends the capabilities of the underlying BibernateSession.
Since:
1.0
Author:
Blyzhnytsia Team
  • Field Details

    • DOT

      private static final String DOT
      Separator used in constructing cache keys.
      See Also:
    • SEPARATOR

      private static final String SEPARATOR
      A constant String representing the separator used for string concatenation or splitting.
      See Also:
    • bibernateSession

      private final BibernateSession bibernateSession
      The underlying BibernateSession implementation.
    • distributedSet

      private final DistributedSet distributedSet
      The distributed set used for caching entities.
  • Constructor Details

    • BibernateSecondLevelCacheSession

      public BibernateSecondLevelCacheSession()
  • Method Details

    • findById

      public <T> Optional<T> findById(Class<T> entityClass, Object primaryKey)
      Retrieves an entity by its primary key, optionally caching it if it's immutable.
      Specified by:
      findById in interface BibernateSession
      Type Parameters:
      T - the type of the entity
      Parameters:
      entityClass - The class of the entity
      primaryKey - The primary key of the entity
      Returns:
      An Optional containing the entity, if found
    • findAll

      public <T> List<T> findAll(Class<T> entityClass)
      Retrieves all entities of a specified type from the data store.
      Specified by:
      findAll in interface BibernateSession
      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

      public <T> List<T> findAllById(Class<T> entityClass, Collection<Object> primaryKeys)
      Retrieves entities based on a collection of primary keys.
      Specified by:
      findAllById in interface BibernateSession
      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)
      Retrieves entities by the value of a specific column.
      Specified by:
      findAllByColumnValue in interface BibernateSession
      Type Parameters:
      T - the type of the entity
      Parameters:
      entityClass - The class of the entity
      columnName - The name of the column
      columnValue - The value of the column
      Returns:
      A list of entities matching the criteria
    • findByWhere

      public <T> List<T> findByWhere(Class<T> entityClass, String whereQuery, Object[] bindValues)
      Retrieves entities based on a custom WHERE clause and bind values.
      Specified by:
      findByWhere in interface BibernateSession
      Type Parameters:
      T - the type of the entity
      Parameters:
      entityClass - The class of the entity
      whereQuery - The WHERE clause
      bindValues - The bind values for parameters in the WHERE clause
      Returns:
      A list of entities matching the criteria
    • findByJoinTableField

      public <T> List<T> findByJoinTableField(Class<T> entityClass, Field field, Object... bindValues)
      Retrieves entities based on a join table field and bind values.
      Specified by:
      findByJoinTableField in interface BibernateSession
      Type Parameters:
      T - the type of the entity
      Parameters:
      entityClass - The class of the entity
      field - The field in the join table
      bindValues - The bind values for parameters in the query
      Returns:
      A list of entities matching the criteria
    • findByWhereJoin

      public <T> Optional<T> findByWhereJoin(Class<T> entityClass, Object[] bindValues)
      Retrieves entities based on a custom query and bind values.
      Specified by:
      findByWhereJoin in interface BibernateSession
      Parameters:
      entityClass - The class of the entity
      bindValues - The bind values for parameters in the query
      Returns:
      A list of entities matching the criteria
    • findByQuery

      public <T> List<T> findByQuery(Class<T> entityClass, String query, Object[] bindValues)
      Retrieves entities based on the provided query and bind values.
      Specified by:
      findByQuery in interface BibernateSession
      Type Parameters:
      T - The type of the entity
      Parameters:
      entityClass - The class of the entity
      query - The query to execute
      bindValues - The bind values for parameters in the query
      Returns:
      A list of entities matching the provided query and bind values
    • update

      public <T> void update(Class<T> entityClass, Object entity)
      Updates the given entity in the database.
      Specified by:
      update in interface BibernateSession
      Type Parameters:
      T - The type of the entity
      Parameters:
      entityClass - The class of the entity
      entity - The entity to update
    • find

      public int find(String query, Object[] bindValues)
      Finds entities based on the provided query and bind values.
      Specified by:
      find in interface BibernateSession
      Parameters:
      query - The query to execute
      bindValues - The bind values for parameters in the query
      Returns:
      The number of entities found
    • save

      public <T> T save(Class<T> entityClass, T entity)
      Saves the provided entity of the specified class.
      Specified by:
      save in interface BibernateSession
      Type Parameters:
      T - The type of the entity
      Parameters:
      entityClass - The class of the entity to save
      entity - The entity to save
      Returns:
      The saved entity
    • saveAll

      public <T> void saveAll(Class<T> entityClass, Collection<T> entity)
      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:
      saveAll in interface BibernateSession
      Type Parameters:
      T - The type of the entity
      Parameters:
      entityClass - The class of the entity
      entity - The entity to save
    • flush

      public void flush()
      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 interface BibernateSession
    • deleteById

      public <T> void deleteById(Class<T> entityClass, Object primaryKey)
      Deletes an entity by its primary key.
      Specified by:
      deleteById in interface BibernateSession
      Type Parameters:
      T - The type of the entity
      Parameters:
      entityClass - The class of the entity
      primaryKey - The primary key of the entity to delete
    • deleteAllById

      public <T> void deleteAllById(Class<T> entityClass, Collection<Object> primaryKeys)
      Deletes entities by their primary keys.
      Specified by:
      deleteAllById in interface BibernateSession
      Type Parameters:
      T - The type of the entity
      Parameters:
      entityClass - The class of the entity
      primaryKeys - The collection of primary keys for entities to delete
    • deleteByColumnValue

      public <T> List<T> deleteByColumnValue(Class<T> entityClass, String columnName, Object columnValue)
      Deletes entities based on the value of a specific column.
      Specified by:
      deleteByColumnValue in interface BibernateSession
      Type Parameters:
      T - The type of the entity
      Parameters:
      entityClass - The class of the entity
      columnName - The name of the column
      columnValue - The value of the column
      Returns:
      A list of entities matching the criteria
    • delete

      public <T> void delete(Class<T> entityClass, T entity)
      Deletes the given entity from the database.
      Specified by:
      delete in interface BibernateSession
      Type Parameters:
      T - The type of the entity
      Parameters:
      entityClass - The class of the entity
      entity - The entity to delete
    • deleteAll

      public <T> void deleteAll(Class<T> entityClass, Collection<T> entities)
      Deletes all entities in the provided collection from the database.
      Specified by:
      deleteAll in interface BibernateSession
      Type Parameters:
      T - The type of the entities
      Parameters:
      entityClass - The class of the entities
      entities - The collection of entities to delete
    • close

      public void close()
      Closes the session and releases any resources associated with it.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface BibernateSession
      Specified by:
      close in interface Closeable
    • getDao

      public Dao getDao()
      Retrieves the DAO associated with this session.
      Specified by:
      getDao in interface BibernateSession
      Returns:
      The DAO associated with this session
    • startTransaction

      public void startTransaction() throws SQLException
      Starts a new transaction for the session using underlying Bibernate session.
      Specified by:
      startTransaction in interface BibernateSession
      Throws:
      SQLException - If an SQL exception occurs while starting the transaction
    • commitTransaction

      public void commitTransaction() throws SQLException
      Commits the current transaction for the session using underlying Bibernate session.
      Specified by:
      commitTransaction in interface BibernateSession
      Throws:
      SQLException - If an SQL exception occurs while committing the transaction
    • rollbackTransaction

      public void rollbackTransaction() throws SQLException
      Rolls back the current transaction for the session using underlying Bibernate session.
      Specified by:
      rollbackTransaction in interface BibernateSession
      Throws:
      SQLException - If an SQL exception occurs while rolling back the transaction