JPA Basics: @Entity, @Id, @GeneratedValue
Your Java class becomes a database table with just a few annotations.
Until now, we saved our API data inside an ArrayList. But if you restart your Spring Boot app, all data is lost. A real backend needs a database. Spring Boot uses Spring Data JPA to talk to SQL databases (like MySQL, PostgreSQL) using Java objects instead of writing raw SQL queries.
The Hyderabad IT Employee ID Card Analogy:
When a new developer joins an IT firm in HITEC City, they don't just exist as a name on a piece of paper. HR creates a profile for them in the company database and assigns a unique Employee ID badge (like EMP-101).
In Spring Boot, ORM (Object-Relational Mapping) works the same way. You write a normal Java class, but you mark it as an @Entity. This tells Spring: "This Java class is a Database Table." The @Id annotation acts as the unique Employee ID badge (Primary Key), and @GeneratedValue tells the database to automatically increment the ID number whenever a new record is created.
- @Entity: Tells Spring Boot/JPA to map this Java class to a database table. By default, the table name matches the class name.
- @Id: Marks a field as the Primary Key.
- @GeneratedValue(strategy = GenerationType.IDENTITY): Tells the database to auto-increment the ID column (e.g., 1, 2, 3...) automatically. You don't pass an ID when creating an object; the database gives it one.
First, we need to configure H2 in application.properties.
# Enable H2 Console in the browser
spring.h2.console.enabled=true
# URL to connect to the in-memory DB
spring.datasource.url=jdbc:h2:mem:devinhyderabad
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
# Hibernate should auto-create tables based on Entity classes
spring.jpa.hibernate.ddl-auto=updatepackage com.devinhyderabad;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
// 1. Maps this class to a database table named "Book"
@Entity
public class Book {
// 2. This is the Primary Key
@Id
// 3. Database will auto-generate IDs (1, 2, 3...)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
// Default constructor is REQUIRED by JPA/Hibernate
public Book() {}
public Book(String title, String author) {
this.title = title;
this.author = author;
}
// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
}If you run this app, Spring Boot will automatically create a table called BOOK in the H2 database with columns id, title, and author, even though we never wrote a CREATE TABLE SQL command!
Interview Question: "What is the difference between GenerationType.IDENTITY and GenerationType.SEQUENCE?"
Answer: IDENTITY relies on the database table's auto-increment feature (like MySQL AUTO_INCREMENT). The database assigns the ID only at the time of the actual INSERT SQL execution. SEQUENCE uses a separate database object (like a counter) to generate IDs before the insert. Hibernate prefers SEQUENCE because it allows pre-allocation of IDs, which improves batch-insert performance.
Enterprise Note: In enterprise apps, we use spring.jpa.hibernate.ddl-auto=update only for local development. In production, using Hibernate to create/alter tables is dangerous and banned. We use Flyway or Liquibase (covered in Chapter 7) to version-control database changes manually.
Key Takeaways
- ✅ @Entity maps a Java class to a database table
- ✅ @Id marks the Primary Key field
- ✅ @GeneratedValue auto-increments the ID column
- ✅ ORM bridges Java objects and SQL tables seamlessly
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login