Class CloseBibernateSession

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

public class CloseBibernateSession extends Object implements BibernateSession
The 'CloseBibernateSession' class extends the BibernateSession and implements session management by delegating calls to an underlying BibernateSession while maintaining the ability to close the session. It introduces a closed flag to track the session state and prevent calls to the database after closure. Additionally, it logs warnings when attempting to modify immutable entities and handles the session's lifecycle methods such as starting, committing, and rolling back transactions.
Since:
1.0
Author:
Blyzhnytsia Team
  • Field Details

    • IMMUTABLE_ENTITY_S_NOT_ALLOWED_TO_CHANGE

      private static final String IMMUTABLE_ENTITY_S_NOT_ALLOWED_TO_CHANGE
      See Also:
    • SESSION_IS_CLOSED_UNABLE_TO_PERFORM_CALLS_TO_THE_DATABASE

      private static final String SESSION_IS_CLOSED_UNABLE_TO_PERFORM_CALLS_TO_THE_DATABASE
      See Also:
    • bibernateSession

      private final BibernateSession bibernateSession
    • closed

      private boolean closed
  • Constructor Details

    • CloseBibernateSession

      public CloseBibernateSession()
  • Method Details

    • findById

      public <T> Optional<T> findById(Class<T> entityClass, Object primaryKey)
      Description copied from interface: BibernateSession
      Finds an entity by its primary key.
      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 found entity, or empty if not found
    • findAll

      public <T> List<T> findAll(Class<T> entityClass)
      Description copied from interface: BibernateSession
      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)
      Description copied from interface: BibernateSession
      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)
      Description copied from interface: BibernateSession
      Finds all entities of a given class with a specified column value.
      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 to match
      columnValue - the value to match
      Returns:
      a list of entities matching the criteria
    • findByWhere

      public <T> List<T> findByWhere(Class<T> entityClass, String whereQuery, Object[] bindValues)
      Description copied from interface: BibernateSession
      Finds entities of a given class based on a custom WHERE clause.
      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 to apply
      bindValues - values to bind to the query parameters
      Returns:
      a list of entities matching the criteria
    • findByJoinTableField

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

      public <T> Optional<T> findByWhereJoin(Class<T> entityClass, Object[] bindValues)
      Specified by:
      findByWhereJoin in interface BibernateSession
    • findByQuery

      public <T> List<T> findByQuery(Class<T> entityClass, String query, Object[] bindValues)
      Description copied from interface: BibernateSession
      Finds entities of a given class based on a custom query.
      Specified by:
      findByQuery in interface BibernateSession
      Type Parameters:
      T - the type of the entity
      Parameters:
      entityClass - the class of the entity
      query - the custom query to execute
      bindValues - values to bind to the query parameters
      Returns:
      a list of entities matching the criteria
    • update

      public <T> void update(Class<T> entityClass, Object entity)
      Description copied from interface: BibernateSession
      Updates an entity in the data store.
      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)
      Description copied from interface: BibernateSession
      Finds the number of records returned by a custom query.
      Specified by:
      find in interface BibernateSession
      Parameters:
      query - the custom query to execute
      bindValues - values to bind to the query parameters
      Returns:
      the number of records returned by the query
    • save

      public <T> T save(Class<T> entityClass, T entity)
      Description copied from interface: BibernateSession
      Saves an entity to the data store.
      Specified by:
      save in interface BibernateSession
      Type Parameters:
      T - the type of the entity
      Parameters:
      entityClass - the class of the entity
      entity - the entity to save
      Returns:
      the saved entity
    • saveAll

      public <T> void saveAll(Class<T> entityClass, Collection<T> entity)
      Description copied from interface: BibernateSession
      Saves a collection of entities into the specified table.
      Specified by:
      saveAll in interface BibernateSession
      Type Parameters:
      T - The generic type representing the entity class.
      Parameters:
      entityClass - The Class object representing the type of entities to be saved.
      entity - A collection of entities to be persisted into the database.
    • deleteById

      public <T> void deleteById(Class<T> entityClass, Object primaryKey)
      Description copied from interface: BibernateSession
      Deletes an entity from the data store 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)
      Description copied from interface: BibernateSession
      Deletes records from the specified table based on the provided collection of primary key values.
      Specified by:
      deleteAllById in interface BibernateSession
      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

      public <T> List<T> deleteByColumnValue(Class<T> entityClass, String columnName, Object columnValue)
      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 interface BibernateSession
      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

      public <T> void delete(Class<T> entityClass, T entity)
      Description copied from interface: BibernateSession
      Deletes an entity from the data store.
      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)
      Description copied from interface: BibernateSession
      Deletes all records associated with the provided entities from the specified table.
      Specified by:
      deleteAll in interface BibernateSession
      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 interface BibernateSession
    • close

      public void close()
      Description copied from interface: BibernateSession
      Closes the session, releasing 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()
      Description copied from interface: BibernateSession
      Gets the Data Access Object (DAO) associated with this session.
      Specified by:
      getDao in interface BibernateSession
      Returns:
      the DAO instance
    • startTransaction

      public void startTransaction() throws SQLException
      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 interface BibernateSession
      Throws:
      SQLException - if a database access error occurs or this method is called on a closed connection
    • commitTransaction

      public void commitTransaction() throws SQLException
      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 interface BibernateSession
      Throws:
      SQLException - if a database access error occurs, the connection is closed or this method is called when no transaction is active
    • rollbackTransaction

      public void rollbackTransaction() throws SQLException
      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 interface BibernateSession
      Throws:
      SQLException - if a database access error occurs, the connection is closed or this method is called when no transaction is active
    • verifySessionNotClosed

      private void verifySessionNotClosed()