POJO vs Entity in Java and Spring Boot

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 vs Entity

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

FeaturePOJO (Plain Old Java Object)Entity (JPA Mapped Object)
PurposeGeneral-purpose data holderRepresents a database table
Annotations RequiredNoYes (@Entity, @Id, etc.)
Persistence LogicNoneManaged by JPA/Hibernate
Used InDTOs, View Models, Business LogicData Persistence (JPA repositories)
Framework DependencyNoYes (JPA, Hibernate, Spring Data)
Database MappingNot mappedMapped to relational database table
Primary Key RequiredNoYes (@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.

Leave a Comment