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)