Class PersistenceFacade

java.lang.Object
org.litebridge.orm.persistence.PersistenceFacade

public class PersistenceFacade extends Object
The PersistenceFacade class provides an abstraction layer for managing the persistence of Data Transfer Objects (DTOs) within a database system. It simplifies interactions with the database, supporting operations such as saving, inserting, and updating records while ensuring consistency with the underlying ORM table definitions.

This class relies on a TableRegistry for mapping DTOs to their corresponding ORM tables and a DatabaseProvider for executing database statements. It encapsulates complex operations, including statement preparation, dependency resolution, primary key updates, and transaction management.

  • Constructor Details

    • PersistenceFacade

      public PersistenceFacade(TableRegistry tableRegistry, TransactionalDatabaseProvider databaseProvider, ChangeTracker changeTracker, DtoConstructor dtoConstructor)
      Constructs a new PersistenceFacade instance.
      Parameters:
      tableRegistry - the registry of ORM tables
      databaseProvider - the database provider
      changeTracker - the change tracker
      dtoConstructor - the DTO constructor
  • Method Details

    • save

      public <DTO> void save(Collection<DTO> dtos) throws SQLException
      Saves a collection of Data Transfer Objects (DTOs) to the database. Each DTO in the collection is processed sequentially, and the save operation determines whether to insert or update the DTO based on its current state. If the operation generates primary keys (e.g., in the case of an insert), those keys are updated in the respective DTOs.
      Type Parameters:
      DTO - the type of the DTOs
      Parameters:
      dtos - the collection of Data Transfer Objects to be saved in the database. Each DTO must correspond to a registered ORM table.
      Throws:
      SQLException - if a database access error occurs during any of the save operations.
    • save

      public <DTO> void save(DTO dto) throws SQLException
      Saves the given Data Transfer Object (DTO) to the database. This method determines whether the DTO should be inserted or updated based on its current state. If the operation generates primary keys (e.g., in the case of an insert), those keys are updated in the DTO.
      Type Parameters:
      DTO - the type of the DTO
      Parameters:
      dto - the Data Transfer Object to be saved in the database. It must correspond to a registered ORM table.
      Throws:
      SQLException - if a database access error occurs during the save operation.
    • insert

      public void insert(Object dto) throws SQLException
      Inserts the specified Data Transfer Object (DTO) into the database. This method constructs an SQL insert statement based on the provided DTO and executes it. If the insertion generates primary keys, those keys will be updated in the corresponding fields of the DTO.
      Parameters:
      dto - the Data Transfer Object to be inserted into the database. It must correspond to a registered ORM table.
      Throws:
      SQLException - if a database access error occurs during the insertion process.
    • update

      public void update(Object dto) throws SQLException
      Updates the specified Data Transfer Object (DTO) in the corresponding database table. This method constructs an SQL update statement based on the provided DTO and executes it. The update operation modifies rows in the database that match the DTO's conditions, such as its primary key or other specified filters.
      Parameters:
      dto - the Data Transfer Object to be updated in the database. It must correspond to a registered ORM table and include valid fields or conditions to perform the update.
      Throws:
      SQLException - if a database access error occurs during the update process.
    • delete

      public void delete(Object dto) throws SQLException
      Deletes the specified Data Transfer Object (DTO) from the corresponding database table.
      Parameters:
      dto - the Data Transfer Object to be deleted from the database
      Throws:
      SQLException - if a database access error occurs during the delete process