What is @Entity Annotation in JPA?
@Entity is a JPA annotation used to mark a Java class as a database entity.
It tells JPA and Hibernate that the class should be mapped to a relational database table.
In simple words:
βThis Java class represents a table in the database.β
What Does JPA Mean?
JPA stands for:
Java Persistence API
JPA is used for Object Relational Mapping (ORM) between Java objects and relational database tables.
Why @Entity Annotation is Needed
Without @Entity:
- JPA cannot recognize the class as a database entity
- Hibernate will not map the class to a table
- CRUD operations cannot be performed automatically
The @Entity annotation enables:
- Automatic table mapping
- Object persistence
- CRUD operations
- Relationship mapping
Simple Real-Time Example
Suppose a database contains:
students
table.
Then we create:
Student.java
entity class and mark it using:
@Entity
Basic @Entity Example
@Entity
public class Student {
@Id
private Long id;
private String name;
private String email;
}
What Happens Internally?
When Spring Boot starts:
- Hibernate scans entity classes
- @Entity classes are detected
- Entities are mapped to database tables
- SQL queries are generated automatically
How @Entity Maps Data
| Java Class | Database |
|---|---|
| Student Class | students Table |
| id Field | id Column |
| name Field | name Column |
| email Field | email Column |
Important Requirements for Entity Classes
JPA entity classes should follow some rules.
Rules for Entity Classes
- Class must be annotated with @Entity
- Class must contain a primary key
- Primary key should use @Id
- Class should have default constructor
- Class should not be final
Primary Key Requirement
Every entity must contain:
@Id
annotation to identify the primary key.
Primary Key Example
@Id
private Long id;
What is @Table Annotation?
@Table specifies the table name explicitly.
@Entity with @Table Example
@Entity
@Table(name = "students")
public class Student {
@Id
private Long id;
private String name;
}
What Happens Without @Table?
If @Table is not provided,
Hibernate uses the class name as the table name by default.
Example:
Student β student
What is @Column Annotation?
@Column maps a field to a specific database column.
@Column Example
@Column(name = "student_email")
private String email;
Auto ID Generation Example
IDs can be generated automatically using:
@GeneratedValue
Example
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Entity Lifecycle in JPA
Entity objects move through different states.
Entity Lifecycle States
| State | Description |
|---|---|
| Transient | Object created but not saved |
| Persistent | Object managed by Hibernate |
| Detached | Object disconnected from session |
| Removed | Object marked for deletion |
Repository Example
public interface StudentRepository
extends JpaRepository<Student, Long> {
}
Save Entity Example
@Autowired
private StudentRepository repository;
public Student saveStudent() {
Student student = new Student();
student.setName("Naresh");
student.setEmail("naresh@gmail.com");
return repository.save(student);
}
Generated SQL Example
Hibernate automatically generates:
INSERT INTO students(name, email)
VALUES (?, ?)
Entity Relationships
Entities support relationships between tables.
Relationship Types
| Relationship | Annotation |
|---|---|
| One-to-One | @OneToOne |
| One-to-Many | @OneToMany |
| Many-to-One | @ManyToOne |
| Many-to-Many | @ManyToMany |
One-to-Many Example
@OneToMany(mappedBy = "student")
private List<Course> courses;
Advantages of @Entity Annotation
- Automatic table mapping
- Reduces boilerplate SQL code
- Supports CRUD operations
- Supports relationships
- Simplifies database interaction
Disadvantages of Improper Entity Design
- Poor performance
- Complex object graphs
- Lazy loading issues
- Unnecessary memory usage
Common Mistakes with @Entity
- Missing @Id annotation
- Using final class
- No default constructor
- Improper relationship mapping
- Excessive eager loading
Best Practices for Entity Classes
- Keep entities simple
- Always define primary key
- Use meaningful table names
- Use lazy loading carefully
- Use DTOs for API responses when needed
Real-Time Example in E-Commerce Application
E-commerce application may contain entities such as:
- Product
- Order
- Customer
- Payment
Each entity class is mapped to a database table
using the @Entity annotation.
Difference Between @Entity and @Table
| Feature | @Entity | @Table |
|---|---|---|
| Purpose | Marks class as entity | Specifies table details |
| Mandatory | Yes | No |
| Used On | Class | Class |
Common Interview Questions on @Entity
What is @Entity annotation?
@Entity marks a Java class as a JPA entity that maps to a database table.
Why is @Entity required?
Because Hibernate uses it to identify database entities.
Can an entity exist without @Id?
No, every entity must have a primary key.
What happens if @Table is not used?
Hibernate uses the class name as table name by default.
What is the difference between @Entity and DTO?
Entity maps database tables, while DTO transfers data between layers.
Conclusion
The @Entity annotation is one of the most important annotations
in JPA and Hibernate.
It tells Hibernate to map Java classes to relational database tables automatically.
@Entity simplifies:
- Database interaction
- CRUD operations
- Relationship mapping
- Object persistence
Understanding @Entity is essential for Spring Boot developers
because modern enterprise applications heavily rely on JPA and Hibernate
for database management.