A Spring Boot application that optimizes database query performance by fetching only the required fields instead of entire entities, reducing data transfer overhead for large datasets.
Traditional JPA queries fetch all entity columns even when only a few fields are needed, resulting in unnecessary data transfer, increased memory consumption, and slower query execution for large datasets.
This project enables dynamic field selection at runtime - fetch only the columns you need:
POST /api/dynamic-query
{
"entityClass": "io.dynamicquery.entity.UserEntity",
"requiredFields": ["username", "email"],
"filters": {"isActive": true}
}Result: Only requested columns are fetched from the database.
git clone <repository-url>
cd dynamic-query-engine
mvn clean install
mvn spring-boot:runcurl -X POST http://localhost:8080/api/dynamic-query \
-H "Content-Type: application/json" \
-d '{
"entityClass": "io.dynamicquery.entity.UserEntity",
"requiredFields": ["username", "email"],
"filters": {"isActive": true}
}'Response:
[
{"username": "john_doe", "email": "john@example.com"},
{"username": "jane_smith", "email": "jane@example.com"}
]- β Selective Field Querying - Fetch only required columns
- β Performance Optimization - Reduce data transfer for large datasets
- β Two Metadata Strategies - Reflection (baseline) or compile-time generation (optimized)
- β
Dynamic Filtering - Support for
=andINclauses - β Type-Safe - Field validation against entity metadata
- β SQL Injection Safe - Parameterized queries
- β Generic - Works with any JPA entity
- Extracts table/column names from JPA annotations at runtime
- Works with any JPA entity without special configuration
- Suitable for development and prototyping
- Generates metadata classes at build time using annotation processor
- Entities marked with
@DynamicQueryabletrigger code generation - Zero runtime reflection - recommended for production
- Client specifies entity, required fields, and filters
- System resolves field β column mappings
- Builds
SELECT field1, field2 FROM table WHERE ... - Executes parameterized query via JPA
- Returns results as flexible JSON maps
Entity: 20 columns, 10,000 rows
Needed: 3 columns
| Approach | Data Transfer | Memory | Query Time |
|---|---|---|---|
| Traditional JPA | ~2 MB | High | Slower |
| Dynamic Query | ~300 KB | Low | Faster |
Indicative savings: ~85% reduction in data transfer
Note: Actual performance gains depend on entity size, dataset volume, and network conditions.
Add @DynamicQueryable to entities:
@DynamicQueryable // Triggers metadata generation at build time
@Entity
@Table(name = "users")
public class UserEntity {
@Id
@Column(name = "user_id")
private Long userId;
@Column(name = "username")
private String username;
}Build project:
mvn clean compileGenerated files:
UserEntity_Metadata.java- Static metadata constantsDynamicEntityRegistry.java- Centralized registry
# Optional: Configure base package for reflection strategy
dynamic:
entity:
base-package: io.dynamicquery.entity{
"entityClass": "io.dynamicquery.entity.ItemEntity",
"requiredFields": ["itemCode", "itemName"],
"filters": {}
}{
"entityClass": "io.dynamicquery.entity.ItemEntity",
"requiredFields": ["itemCode", "itemName", "unitPrice"],
"filters": {"category": "Electronics"}
}{
"entityClass": "io.dynamicquery.entity.ItemEntity",
"requiredFields": ["itemCode", "itemName"],
"filters": {"category": ["Electronics", "Furniture"]}
}{
"entityClass": "io.dynamicquery.entity.ItemEntity",
"requiredFields": ["itemCode", "itemName", "category"],
"filters": {
"category": "Electronics",
"isActive": true
}
}# Run all tests
mvn test
# Run specific test
mvn test -Dtest=DynamicEntityRegistryTest- PROJECT_GUIDE.md - Complete technical guide
- DYNAMIC_QUERY_TEST_PAYLOADS.md - 50+ test examples
- Reporting Systems - Fetch only display columns
- API Responses - Return minimal data for mobile clients
- Data Export - Select specific fields for CSV/Excel
- Search Results - Show summary fields only
- Large Datasets - Optimize queries for millions of rows
- SQL Injection Protection - All queries use parameterized statements
- Field Validation - Only valid entity fields can be queried
- Type Safety - Compile-time validation through entity structure
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
This project is licensed under the MIT License.
Built with β€οΈ using Spring Boot and Java