In Java and Spring Boot development, we are using class terms like Plain Old Java Object and Entity are commonly used when working with data and databases in the application. However, they may look similar at first glance, but they serve distinct purposes and are used in different layers of an application. This topic will explain the POJO vs Entity, highlight their roles, and offer best practices for each.
POJO
- It is not dependent on any framework.
- It has no specific constraints or annotations.
- It has hold fields, constructors, getters/setters, and maybe basic utility methods.
Key Characteristics of POJO
- It is a Framework-agnostic (can be used anywhere).
- There is no need for @Entity, @Component, or any annotations.
- This is Ideal for DTOs, helper classes, or general-purpose data holders.
Example
public class User {
private String name;
private String email;
public User() {}
public User(String name, String email) {
this.name = name;
this.email = email;
}
// Getters and Setters
}
Entity
It is also a POJO class that but annotated with JPA annotations to map this class into the database table.
Key Characteristics of Entity
- Annotated with @Entity.
- Maps Java fields to database columns.
- Requires a primary key (@Id).
- JPA annotations(@Table, @Column, etc.) can be annotated.
Example
import javax.persistence.*;
@Entity
@Table(name = "users")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String email;
private String name;
// Getters and Setters
}
POJO VS Entity
Feature | POJO (Plain Old Java Object) | Entity (JPA Mapped Object) |
Purpose | General-purpose data holder | Represents a database table |
Annotations Required | No | Yes (@Entity, @Id, etc.) |
Persistence Logic | None | Managed by JPA/Hibernate |
Used In | DTOs, View Models, Business Logic | Data Persistence (JPA repositories) |
Framework Dependency | No | Yes (JPA, Hibernate, Spring Data) |
Database Mapping | Not mapped | Mapped to relational database table |
Primary Key Required | No | Yes (@Id field is mandatory) |
When to Use POJO vs Entity
Use POJO When
- Lightweight class to store data.
- Building DTOs, form objects, or API request/response models.
- Want to tie the class to a persistence framework.
Use Entity When
- JPA/Hibernate to persist data.
- To map a class directly to a database table.
- To perform CRUD operations through Spring Data JPA or EntityManager.
Can an Entity Be a POJO?
Yes. Every Entity class is a POJO class. But not every POJO is an Entity class. Entities are JPA annotation annotated POJOs that gain persistence capabilities.
Real-World Usage Example in Spring Boot
Entity Example
@Entity
public class Product {
@Id
private Long id;
private String name;
private double price;
}
DTO (POJO) Example
public class ProductDTO {
private String name;
private double price;
}
- Product is stored in the database.
- ProductDTO is returned to the client via REST API.
Conclusion
In this topic, we learnt about POJO vs Entity in Java and Spring Boot with an example.