Class AbstractRepository<T,I>

java.lang.Object
no.ntnu.principes.repository.AbstractRepository<T,I>
Type Parameters:
T - The entity type this repository manages
I - The type of the entity's identifier
All Implemented Interfaces:
BaseRepository<T,I>
Direct Known Subclasses:
ConfigValueRepository, MemberRepository, PointsRepository, TaskAssignmentRepository, TaskRepository

public abstract class AbstractRepository<T,I> extends Object implements BaseRepository<T,I>
Base implementation of the repository pattern with JDBC. Provides reusable database operations for concrete repositories. Handles connection management, statement preparation, and result set processing.
  • Field Details

    • dataSource

      protected final DataSource dataSource
    • tableName

      protected final String tableName
  • Constructor Details

    • AbstractRepository

      protected AbstractRepository(DataSource dataSource, String tableName)
      Creates a new repository for the specified table.
      Parameters:
      dataSource - The JDBC data source for database connections
      tableName - The name of the database table
  • Method Details

    • queryOne

      protected Optional<T> queryOne(String sql, Object... params)
      Executes a query expecting a single result.
      Parameters:
      sql - The SQL query to execute
      params - The parameters to bind to the query
      Returns:
      An Optional containing the mapped entity or empty if no results
      Throws:
      RuntimeException - If a database error occurs
    • queryList

      protected List<T> queryList(String sql, Object... params)
      Executes a query expecting multiple results.
      Parameters:
      sql - The SQL query to execute
      params - The parameters to bind to the query
      Returns:
      A list of mapped entities
      Throws:
      RuntimeException - If a database error occurs
    • executeInsert

      protected long executeInsert(String sql, Object... params)
      Executes an INSERT statement and returns the generated key.
      Parameters:
      sql - The SQL insert statement to execute
      params - The parameters to bind to the statement
      Returns:
      The generated primary key
      Throws:
      RuntimeException - If a database error occurs or key retrieval fails
    • executeUpdate

      protected int executeUpdate(String sql, Object... params)
      Executes an UPDATE or DELETE statement.
      Parameters:
      sql - The SQL statement to execute
      params - The parameters to bind to the statement
      Returns:
      The number of affected rows
      Throws:
      RuntimeException - If a database error occurs
    • mapRow

      protected abstract T mapRow(ResultSet rs) throws SQLException
      Maps a database result set row to an entity object. Implemented by concrete repositories based on their specific entity structure.
      Parameters:
      rs - The result set positioned at the row to map
      Returns:
      The mapped entity object
      Throws:
      SQLException - If a database error occurs during mapping
    • queryOneRaw

      protected Optional<Object[]> queryOneRaw(String sql, Object... params)
      Executes a query for a single row and returns the raw result set.
      Parameters:
      sql - The SQL query
      params - The parameters to bind to the query
      Returns:
      An Optional containing the raw result set or empty if no result
      Throws:
      RuntimeException - If a database error occurs
    • queryOneRaw

      protected Optional<Object[]> queryOneRaw(String sql)
      Executes a query for a single row and returns the raw result set.
      Parameters:
      sql - The SQL query
      Returns:
      An Optional containing the raw result set or empty if no result
      Throws:
      RuntimeException - If a database error occurs
    • queryListRaw

      protected List<Object[]> queryListRaw(String sql)
      Executes a query and returns a list of raw result sets.
      Parameters:
      sql - The SQL query
      Returns:
      A list of result sets
      Throws:
      RuntimeException - If a database error occurs
    • queryListRaw

      protected List<Object[]> queryListRaw(String sql, Object... params)
      Executes a query and returns a list of raw result sets.
      Parameters:
      sql - The SQL query
      params - The parameters to bind to the query
      Returns:
      A list of result sets
      Throws:
      RuntimeException - If a database error occurs