Spring Boot
Capstone – Article 2: Database Configuration & User Entity (Expense Tracker)
By Utility Zone · 2026-01-27T18:31:20.02008
1. Introduction
In this article, we move from empty application to a data-backed application.
Goals of this article:
- Configure database properly
- Understand JPA configuration
- Create the User entity
- Verify database tables
This is the foundation of the entire Expense Tracker.
2. Choosing Database Strategy
Development
- H2 (In-Memory Database)
- Fast
- No installation required
Production (Later)
- PostgreSQL / MySQL
We start with H2 and keep production-ready design in mind.
3. Database Configuration (H2)
Open application.properties and add:
## ===============================
## Database Configuration
## ===============================
spring.datasource.url=jdbc:h2:mem:expensetrackerdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
## ===============================
## JPA Configuration
## ===============================
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
## ===============================
## H2 Console
## ===============================
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
Restart the application after adding this.
4. Verifying H2 Console
Open browser:
http://localhost:8080/h2-console
Use:
- JDBC URL:
jdbc:h2:mem:expensetrackerdb - Username:
sa - Password: (blank)
Click Connect.
5. Creating User Entity
5.1 Create Entity Package
Create package:
com.example.expensetracker.entity
5.2 Create User Entity
package com.example.expensetracker.entity;
import jakarta.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String role;
public User() {}
// getters and setters
}
Why these fields?
email→ login identitypassword→ encrypted laterrole→ authorization (USER,ADMIN)
6. Understanding JPA Annotations
| Annotation | Purpose |
|---|---|
@Entity | Maps class to table |
@Table | Custom table name |
@Id | Primary key |
@GeneratedValue | Auto-generated ID |
@Column | Column constraints |
7. Verifying Table Creation
Restart the app and refresh H2 console.
You should see:
USERS
table created automatically.
This confirms:
✔ JPA is working
✔ Database config is correct
8. Why ddl-auto=update (For Now)
spring.jpa.hibernate.ddl-auto=update
Use this only for:
- Local development
- Early learning
Later we will:
✔ Disable this
✔ Use Flyway migrations
9. Common Beginner Mistakes
❌ Wrong JDBC URL
❌ Missing no-args constructor
❌ Forgetting @Entity
❌ Using reserved table names
10. Git Commit (Important)
Commit your progress:
git add .
git commit -m "Add H2 database configuration and User entity"
Clean commits matter.
11. What You Should Have Now
At this point:
- Database configured
- H2 console accessible
- User table created
- Entity mapped correctly
This is a huge milestone.
12. What’s Next?
➡ Capstone – Article 3: User Repository & Basic CRUD
- JpaRepository
- Save & fetch users
- First real data operations
Type Next when you’re ready 🚀