Demonstrates JPA/Hibernate, Spring Data JPA, JDBC, connection pooling, and transaction management.
src/main/java/com/example/template/database/
├── entity/
│ ├── Order.java # JPA entity with @OneToMany, @Version, lifecycle callbacks
│ ├── OrderItem.java # Child entity with @ManyToOne
│ └── OrderStatus.java # Enum mapped with @Enumerated
├── repository/
│ └── OrderRepository.java # Spring Data JPA: derived queries, JPQL, native SQL, bulk updates
├── service/
│ └── OrderService.java # @Transactional: propagation, isolation, read-only, rollback rules
├── jdbc/
│ └── JdbcOrderDao.java # JdbcTemplate, NamedParameterJdbcTemplate, SimpleJdbcInsert, batch ops
├── config/
│ └── DataSourceConfig.java # Programmatic HikariCP configuration
└── DatabaseExampleApplication.java
src/main/resources/
├── application.yml # DataSource, HikariCP, JPA, Flyway config (H2 + PostgreSQL profiles)
└── db/migration/
└── V1__create_orders.sql # Flyway migration script
- Entity mapping with
@Entity,@Table,@Column - Relationships:
@OneToMany/@ManyToOnewith cascade and orphan removal - Optimistic locking with
@Version - Lifecycle callbacks:
@PrePersist,@PreUpdate - Fetch strategies:
FetchType.LAZYwithJOIN FETCHfor eager loading when needed
- Derived query methods (convention-based)
@Querywith JPQL and native SQL@Modifyingfor bulk updates@Paramfor named parameters
- Class-level
@Transactional(readOnly = true)with method-level overrides - Propagation:
REQUIRED(default),REQUIRES_NEW(independent transaction) - Isolation:
REPEATABLE_READfor consistent reads - Rollback rules:
rollbackFor = Exception.class
JdbcTemplate— positional parameters,queryForObject,batchUpdateNamedParameterJdbcTemplate— named parameters withMapSqlParameterSourceSimpleJdbcInsert— fluent insert with auto-generated keysRowMapper— reusable result set mapping
- Pool sizing:
maximumPoolSize,minimumIdle - Timeouts:
connectionTimeout,idleTimeout,maxLifetime - Leak detection:
leakDetectionThreshold - Profile-specific tuning (dev vs production)
- Versioned migrations:
V{version}__{description}.sql baseline-on-migratefor existing databases- Profile-specific migration locations
| Profile | Database | Pool Size | DDL Strategy |
|---|---|---|---|
| default | H2 (mem) | 10 | validate |
| production | PostgreSQL | 20 | validate |
- Disable OSIV —
spring.jpa.open-in-view: falseprevents lazy loading outside transactions - Flyway for DDL —
ddl-auto: validateensures Hibernate matches Flyway-managed schema - Batch operations —
hibernate.jdbc.batch_size+order_insertsfor bulk performance - N+1 prevention —
default_batch_fetch_sizeand explicitJOIN FETCHqueries - Read-only transactions — class-level default reduces lock overhead for reads
- Leak detection — HikariCP warns when connections aren't returned promptly
- Main README — Project overview and quick start
- Best Practices — Resource management, error handling
- Third-Party Libraries — HikariCP, Flyway reference
- Architecture Patterns — Layer design and project structure
- Tutorial — New developer walkthrough