java.lang.Object
no.ntnu.principes.repository.AbstractRepository<T,I>
- Type Parameters:
T- The entity type this repository managesI- The type of the entity's identifier
- All Implemented Interfaces:
BaseRepository<T,I>
- Direct Known Subclasses:
ConfigValueRepository,MemberRepository,PointsRepository,TaskAssignmentRepository,TaskRepository
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 Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractRepository(DataSource dataSource, String tableName) Creates a new repository for the specified table. -
Method Summary
Modifier and TypeMethodDescriptionprotected longexecuteInsert(String sql, Object... params) Executes an INSERT statement and returns the generated key.protected intexecuteUpdate(String sql, Object... params) Executes an UPDATE or DELETE statement.protected abstract TMaps a database result set row to an entity object.Executes a query expecting multiple results.queryListRaw(String sql) Executes a query and returns a list of raw result sets.queryListRaw(String sql, Object... params) Executes a query and returns a list of raw result sets.Executes a query expecting a single result.queryOneRaw(String sql) Executes a query for a single row and returns the raw result set.queryOneRaw(String sql, Object... params) Executes a query for a single row and returns the raw result set.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface no.ntnu.principes.repository.BaseRepository
deleteById, findAll, findById, save
-
Field Details
-
dataSource
-
tableName
-
-
Constructor Details
-
AbstractRepository
Creates a new repository for the specified table.- Parameters:
dataSource- The JDBC data source for database connectionstableName- The name of the database table
-
-
Method Details
-
queryOne
Executes a query expecting a single result.- Parameters:
sql- The SQL query to executeparams- 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
Executes a query expecting multiple results.- Parameters:
sql- The SQL query to executeparams- The parameters to bind to the query- Returns:
- A list of mapped entities
- Throws:
RuntimeException- If a database error occurs
-
executeInsert
Executes an INSERT statement and returns the generated key.- Parameters:
sql- The SQL insert statement to executeparams- The parameters to bind to the statement- Returns:
- The generated primary key
- Throws:
RuntimeException- If a database error occurs or key retrieval fails
-
executeUpdate
Executes an UPDATE or DELETE statement.- Parameters:
sql- The SQL statement to executeparams- The parameters to bind to the statement- Returns:
- The number of affected rows
- Throws:
RuntimeException- If a database error occurs
-
mapRow
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
Executes a query for a single row and returns the raw result set.- Parameters:
sql- The SQL queryparams- 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
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
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
Executes a query and returns a list of raw result sets.- Parameters:
sql- The SQL queryparams- The parameters to bind to the query- Returns:
- A list of result sets
- Throws:
RuntimeException- If a database error occurs
-