|
| 1 | +# Theory (Java) |
| 2 | + |
| 3 | +[](https://github.com/OWNER/REPO/actions/workflows/ci.yml) |
| 4 | +[](https://central.sonatype.com/artifact/io.retrorock/theory) |
| 5 | + |
| 6 | +Note: Replace `OWNER/REPO` with your GitHub org/repo, and adjust Maven Central coordinates when published. |
| 7 | + |
| 8 | +A small, annotation-driven data access library built on Spring JDBC. It provides: |
| 9 | + |
| 10 | +- Model mapping via annotations (`@Table`, `@Column`, `@PrimaryKey`, `@BelongsTo`) on POJOs. |
| 11 | +- A lightweight `Repository<T>` base with common query and persistence helpers. |
| 12 | +- A simple SQL builder through `Query` and `ModelMapper` to generate SELECTs with joins and limits. |
| 13 | + |
| 14 | +This is intended for pragmatic, minimal data access without a heavy ORM. |
| 15 | + |
| 16 | +## Requirements |
| 17 | + |
| 18 | +- Java 8+ |
| 19 | +- Maven 3+ |
| 20 | +- Spring JDBC 4.2.x (pulled via `pom.xml`) |
| 21 | + |
| 22 | +## Project Structure |
| 23 | + |
| 24 | +- `pom.xml` — Maven build and dependencies |
| 25 | +- `src/main/java/io/retrorock/theory/` — library source |
| 26 | + - `annotations/` — `@Table`, `@Column`, `@PrimaryKey`, `@BelongsTo` |
| 27 | + - `base/` — `Model`, `Repository`, `Operation` |
| 28 | + - `components/` — SQL building blocks (e.g., `From`, `Join`) |
| 29 | + - `helpers/` — mappers and utilities (e.g., `ModelMapper`, `RowMapperHelper`) |
| 30 | + - `interfaces/` — `IRepository`, `IOperation` |
| 31 | + - `operations/` — `Query`, `Insert`, `Update`, `Delete` |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +Build and install to your local Maven repository: |
| 36 | + |
| 37 | +```bash |
| 38 | +mvn clean install |
| 39 | +``` |
| 40 | + |
| 41 | +Then, in a separate project, add the dependency (adjust version if needed): |
| 42 | + |
| 43 | +```xml |
| 44 | +<dependency> |
| 45 | + <groupId>io.retrorock</groupId> |
| 46 | + <artifactId>theory</artifactId> |
| 47 | + <version>1.0-SNAPSHOT</version> |
| 48 | +</dependency> |
| 49 | +``` |
| 50 | + |
| 51 | +## Quick Start |
| 52 | + |
| 53 | +### 1) Define a Model |
| 54 | + |
| 55 | +Annotate your entity with table/column mapping. Extend `Model` to get helpers like `serialize()` and JDBC `RowMapper` behavior. |
| 56 | + |
| 57 | +```java |
| 58 | +import io.retrorock.theory.annotations.*; |
| 59 | +import io.retrorock.theory.base.Model; |
| 60 | + |
| 61 | +@Table(name = "users", alias = "u") |
| 62 | +public class User extends Model { |
| 63 | + @PrimaryKey |
| 64 | + @Column(name = "id", alias = "u_id") |
| 65 | + private Integer id; |
| 66 | + |
| 67 | + @Column(name = "email", alias = "u_email") |
| 68 | + private String email; |
| 69 | + |
| 70 | + @Column(name = "name", alias = "u_name") |
| 71 | + private String name; |
| 72 | + |
| 73 | + // getters/setters ... |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Relationships can be declared with `@BelongsTo` on a field referencing another `Model`. |
| 78 | + |
| 79 | +### 2) Create a Repository |
| 80 | + |
| 81 | +Extend `Repository<T>` and wire a `JdbcTemplate` (via Spring or manually through `JdbcDaoSupport`). |
| 82 | + |
| 83 | +```java |
| 84 | +import io.retrorock.theory.base.Repository; |
| 85 | +import org.springframework.jdbc.core.RowMapper; |
| 86 | +import org.springframework.jdbc.core.BeanPropertyRowMapper; |
| 87 | + |
| 88 | +public class UserRepository extends Repository<User> { |
| 89 | + public UserRepository() { |
| 90 | + this.setEntity(User.class); |
| 91 | + this.setMapper((RowMapper<User>) new BeanPropertyRowMapper<>(User.class)); |
| 92 | + this.setTableName("users"); |
| 93 | + this.setPrimaryKeyName("id"); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +Inject a `DataSource` so `JdbcDaoSupport` can provide `JdbcTemplate`: |
| 99 | + |
| 100 | +```java |
| 101 | +import javax.sql.DataSource; |
| 102 | + |
| 103 | +public class UserRepository extends Repository<User> { |
| 104 | + public UserRepository(DataSource dataSource) { |
| 105 | + this(); // calls default ctor to set entity/mapper |
| 106 | + setDataSource(dataSource); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### 3) Querying |
| 112 | + |
| 113 | +Use the built-in `Query` builder. `Repository#list()` and `Repository#find(id)` leverage `Query` and your mapper. |
| 114 | + |
| 115 | +```java |
| 116 | +UserRepository repo = new UserRepository(dataSource); |
| 117 | + |
| 118 | +// Find by primary key |
| 119 | +User u = repo.find(1); |
| 120 | + |
| 121 | +// List all |
| 122 | +List<User> users = repo.list(); |
| 123 | + |
| 124 | +// Custom select with Query |
| 125 | +var q = new io.retrorock.theory.operations.Query(); |
| 126 | +q.db.from(User.class) |
| 127 | + .fields(User.class) |
| 128 | + .where("u.email = '%s'", "alice@example.com") |
| 129 | + .limit(10); |
| 130 | +List<User> result = repo.select(q, repo.mapper); |
| 131 | +``` |
| 132 | + |
| 133 | +Note: `Query#selectString()` prints the generated SQL and resets internal state after building. |
| 134 | + |
| 135 | +### 4) Insert/Update/Delete |
| 136 | + |
| 137 | +`Repository` exposes `persist` helpers that accept an `Operation`: |
| 138 | + |
| 139 | +```java |
| 140 | +User user = new User(); |
| 141 | +user.setEmail("bob@example.com"); |
| 142 | +user.setName("Bob"); |
| 143 | + |
| 144 | +// Insert and return generated key |
| 145 | +Integer id = repo.persist(user, new io.retrorock.theory.operations.Insert()); |
| 146 | +user.identify(id); |
| 147 | + |
| 148 | +// Update example |
| 149 | +var update = new io.retrorock.theory.operations.Update(); |
| 150 | +repo.persist(user, update); |
| 151 | + |
| 152 | +// Raw SQL |
| 153 | +repo.persist("DELETE FROM users WHERE id = 123"); |
| 154 | +``` |
| 155 | + |
| 156 | +## Minimal Runnable Example |
| 157 | + |
| 158 | +This repo includes a minimal example using H2 in-memory DB under `src/test/java/io/retrorock/theory/example/`: |
| 159 | + |
| 160 | +- `User` model: `src/test/java/io/retrorock/theory/example/User.java` |
| 161 | +- `UserRepository`: `src/test/java/io/retrorock/theory/example/UserRepository.java` |
| 162 | +- Test: `src/test/java/io/retrorock/theory/example/UserRepositoryTest.java` |
| 163 | + |
| 164 | +Run just the example test: |
| 165 | + |
| 166 | +```bash |
| 167 | +mvn -Dtest=io.retrorock.theory.example.UserRepositoryTest test |
| 168 | +``` |
| 169 | + |
| 170 | +## Spring Configuration |
| 171 | + |
| 172 | +In XML or Java config, provide a `DataSource` and wire your repository. Example (Java config): |
| 173 | + |
| 174 | +```java |
| 175 | +import org.springframework.context.annotation.*; |
| 176 | +import org.springframework.jdbc.datasource.DriverManagerDataSource; |
| 177 | + |
| 178 | +@Configuration |
| 179 | +public class AppConfig { |
| 180 | + @Bean |
| 181 | + public DataSource dataSource() { |
| 182 | + DriverManagerDataSource ds = new DriverManagerDataSource(); |
| 183 | + ds.setDriverClassName("org.postgresql.Driver"); |
| 184 | + ds.setUrl("jdbc:postgresql://localhost:5432/app"); |
| 185 | + ds.setUsername("app"); |
| 186 | + ds.setPassword("secret"); |
| 187 | + return ds; |
| 188 | + } |
| 189 | + |
| 190 | + @Bean |
| 191 | + public UserRepository userRepository(DataSource ds) { |
| 192 | + return new UserRepository(ds); |
| 193 | + } |
| 194 | +} |
| 195 | +``` |
| 196 | + |
| 197 | +## Development |
| 198 | + |
| 199 | +- Build: `mvn clean package` |
| 200 | +- Tests: `mvn test` |
| 201 | + |
| 202 | +### TDD Flow (suggested) |
| 203 | + |
| 204 | +1. Write a failing test under `src/test/java/` that describes desired behavior. |
| 205 | +2. Run tests: `mvn test` and watch it fail. |
| 206 | +3. Implement the minimal code in `src/main/java/` to make it pass. |
| 207 | +4. Refactor ruthlessly while keeping tests green. |
| 208 | +5. Repeat. |
| 209 | + |
| 210 | +## Notes |
| 211 | + |
| 212 | +- This library targets Spring 4.2.x APIs as specified in `pom.xml`. |
| 213 | +- Some operations rely on column aliases matching the `@Column(alias=...)` convention during mapping. |
| 214 | + |
| 215 | +## License |
| 216 | + |
| 217 | +MIT — see `LICENSE` for details. |
0 commit comments