Key JPA Annotations: @Entity, @Id, and @GeneratedValue
In Spring Data JPA, @Entity, @Id, and @GeneratedValue are core annotations that form the foundation of ORM (Object-Relational Mapping). They are essential for mapping Java classes to database tables, defining primary keys, and automating ID generation, enabling clean, maintainable, and scalable applications.
@Entity
Marks a Java class as a JPA entity, meaning it represents a table in the database.
Key Points:
- Every class annotated with
@Entitymaps to a database table. - Must have a no-argument constructor (public or protected).
- The default table name is the class name, but it can be overridden using
@Table(name="table_name"). - Can define relationships using annotations like
@OneToMany,@ManyToOne, etc.
Example:
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
@Entity
@Table(name = "users")// Optional
publicclassUser {
private Long id;
private String name;
private String email;
// Getters and setters
}
ā
@Entity tells JPA/Hibernate to manage this class as a database table.
@Id
Specifies the primary key of an entity.
Key Points:
- Every entity must have a field annotated with
@Id. - The primary key uniquely identifies each record in the table.
- Can be combined with
@GeneratedValuefor automatic generation.
Example:
import jakarta.persistence.Id;
@Entity
publicclassUser {
@Id
private Long id;
private String name;
}
ā
@Id ensures JPA knows which field is the unique identifier for each record.
@GeneratedValue
Automatically generates values for the primary key field annotated with @Id.
Generation Strategies:
GenerationType.AUTOā JPA provider selects the strategy.GenerationType.IDENTITYā Uses auto-increment columns.GenerationType.SEQUENCEā Uses a database sequence (common in Oracle, PostgreSQL).GenerationType.TABLEā Uses a special table to generate IDs.
Example:
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
@Entity
publicclassUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
ā
@GeneratedValue eliminates the need to manually assign primary key values.
How They Work Together
@Entity
@Table(name = "users")
publicclassUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
}
@Entityā Marks the class as a table.@Idā Specifies the primary key column.@GeneratedValueā Auto-generates the primary key on record insertion.
Summary Table
| Annotation | Purpose | Example Usage |
|---|---|---|
@Entity | Marks class as a database table | @Entity |
@Id | Specifies primary key field | @Id |
@GeneratedValue | Auto-generates primary key values | @GeneratedValue(strategy=IDENTITY) |
Benefits
- Eliminates manual SQL for inserting primary keys.
- Provides automatic object-to-table mapping.
- Simplifies CRUD operations in Spring Data JPA.
- Ensures clean, maintainable, and scalable ORM-based applications.
- Works seamlessly with Spring Boot and Hibernate.
Conclusion
Using @Entity, @Id, and @GeneratedValue together allows developers to map Java objects to database tables effortlessly. These annotations form the backbone of ORM in Spring Data JPA, reducing boilerplate code, improving maintainability, and enabling rapid development of enterprise-grade applications.
