In Java (especially in JPA/Hibernate), an Entity is a Java class that represents a table in a database.
Each object of the entity class corresponds to a row in that table, and each field in the class maps to a column.
Simple Definition
👉 An Entity is a POJO (Plain Old Java Object) that is mapped to a database table using annotations.
Example of an Entity
java id="y9e5c2"
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@ Entity
public class User {
@Id
private int id;
private String name;
private String email;
}
How Mapping Works
| Java Class | Database |
|---|---|
Class (User) |
Table (user) |
| Object | Row |
Field (name) |
Column |
| @ Id | Primary Key |
Important Annotations
@ Entity
Marks the class as a database entity.
@ Id
Specifies the primary key.
@ Table
Used to define table name (optional).
java id="lgbl66"
@entity
@Table(name = "users")
public class User {
}
@ Column
Maps field to a specific column.
Key Characteristics of an Entity
- Must have a primary key (@ Id)
- Should have a no-argument constructor
- Should be a POJO class
- Can include getters and setters
Example Scenario
In a banking application:
-
Customer→ Entity → Customer table -
Account→ Entity → Account table
Each entity represents real-world data stored in the database.
Why Entities are Important
- Simplifies database interaction
- Enables ORM (Object-Relational Mapping)
- Reduces SQL usage
- Improves code readability
Conclusion
An Entity is a core concept in JPA/Hibernate that allows Java developers to map objects to database tables easily. It plays a key role in building database-driven applications using modern frameworks like Spring Boot.
🚀 Learn Java & Backend Development
Build strong fundamentals with Best Core JAVA Online Training.
Top comments (0)