ORMFM: Object-Relational Mapping Framework
A custom ORM framework built from scratch, abstracting JDBC boilerplate via a fluent query builder API and dynamic SQL generation using Java Reflection and custom Annotations.
Overview
ORMFM is a custom Object-Relational Mapping framework written entirely in Java, built to understand how Hibernate and JPA work internally before using them. The framework maps Java objects to database tables, handles transactions, manages sessions in a thread-safe manner, and includes an in-memory caching layer.
The project also ships with ORMFMTool: a companion CLI utility that introspects live database schemas and auto-generates POJO classes and PDF documentation.
Key Technical Contributions
Fluent Query Builder API
Designed a clean, type-safe query builder API that eliminates raw SQL string concatenation and JDBC boilerplate. Queries are constructed using method chaining (select().from().where().build()), with dynamic SQL generation under the hood via Java Reflection.
Thread-Safe Session Management
Engineered a session management system using ThreadLocal<Session> to ensure each thread (each HTTP request in a web context) gets its own isolated database connection. This prevents connection leakage and ensures proper transaction boundaries in multi-threaded environments.
In-Memory Caching Layer (@Cacheable)
Implemented an annotation-driven caching layer using LinkedHashMap with LRU eviction. Methods annotated with @Cacheable serve repeated reads from memory rather than hitting the database, while the cache is automatically invalidated on writes to maintain consistency.
Custom Annotation Schema (@Table, @Column, @Id)
Designed a full set of custom annotations inspired by JPA for defining the mapping between Java classes and database schema, without pulling in any JPA dependency.
ORMFMTool (CLI Utility)
Developed a standalone CLI utility that connects to a live database, reads schema metadata via DatabaseMetaData, and auto-generates:
- Fully mapped POJO classes (
.javafiles) - PDF documentation describing all tables, columns, and constraints
What I Learned
Building ORMFM gave me hands-on experience with concepts that are otherwise completely hidden by Hibernate:
- How
ThreadLocalis used for unit-of-work isolation - How Hibernate’s proxy-based lazy loading works conceptually
- The difference between first-level (session) and second-level (shared) caching
- How connection pooling fits into the ORM lifecycle
Stack
| Layer | Technology |
|---|---|
| Core | Java, JDBC |
| Reflection | java.lang.reflect, Custom Annotations |
| Session Management | ThreadLocal<Session> |
| Caching | LinkedHashMap (LRU) |
| Schema Introspection | java.sql.DatabaseMetaData |
| Documentation | iText PDF |