Database Schema
Entity: User
Table Definition
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Column Details
| Column |
Type |
Constraints |
Description |
| id |
BIGINT |
PRIMARY KEY, AUTO_INCREMENT |
Unique identifier |
| username |
VARCHAR(50) |
NOT NULL, UNIQUE |
User's unique username |
| email |
VARCHAR(100) |
NOT NULL, UNIQUE |
User's email address |
| created_at |
TIMESTAMP |
DEFAULT CURRENT_TIMESTAMP |
Record creation time |
| updated_at |
TIMESTAMP |
DEFAULT CURRENT_TIMESTAMP ON UPDATE |
Last update time |
Indexes
| Index Name |
Column |
Type |
Description |
| idx_username |
username |
UNIQUE |
Fast username lookup |
| idx_email |
email |
UNIQUE |
Fast email lookup |
Entity Relationships
User (standalone entity, no foreign keys)
JPA Entity Mapping
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true, length = 50)
private String username;
@Column(nullable = false, unique = true, length = 100)
private String email;
@Column(name = "created_at", updatable = false)
private LocalDateTime createdAt;
@Column(name = "updated_at")
private LocalDateTime updatedAt;
}
Database Support
| Environment |
Database |
Driver |
| Development |
H2 (In-Memory) |
org.h2.Driver |
| Test |
H2 (In-Memory) |
org.h2.Driver |
| Production |
MySQL 8.x |
com.mysql.cj.jdbc.Driver |
| Production |
PostgreSQL 15+ |
org.postgresql.Driver |
Migration Strategy
- Development: Auto DDL (Hibernate)
- Production: Flyway Migration Scripts
- Location:
src/main/resources/db/migration/
- Naming:
V{version}__{description}.sql