What is POJO?

POJO stands for “Plain Old Java Object.” It is a term used to describe a Java object that is not “polluted” by any special requirements from a framework or a library. It is a simple Java object that does not have any special restrictions or requirements.

In the context of Clean Architecture and DDD, a POJO is an object that:

  1. Doesn’t extend a specific class (e.g., extends HttpServlet or extends BaseEntity).
  2. Doesn’t implement a specific interface from a library (e.g., implements Serializable is often okay, but implements org.hibernate.Entity is not).
  3. Doesn’t use “heavy” annotations that tie it to a technology (like @Entity from JPA or @Table from SQL).

Why use POJOs in your Domain (Domain Driven Diagram)?

  • Technology Independence: Your business logic doesn’t care if you use PostgreSQL, MongoDB, or even if you’re running on a Web Server or a CLI.
  • Easier Testing: You can test a POJO with simple Unit Tests without needing to start a Spring Context or a database.
  • Future Proofing: If you decide to switch from JPA to another database technology later, your core business logic (the POJOs) won’t have to change at all.

Example comparison:

POJO (Clean/DDD style):

public class User {
  private String username;
    // Pure Java logic
  }

NOT a POJO (Infrastructure-coupled style):

@Entity // Tied to JPA
@Table(name = "users") // Tied to SQL
public class User {
  @Id // Tied to JPA
  private Long id;
}

Notes:

By keeping your domain.entities as POJOs, you are ensuring that your “Business Truth” remains pure and independent!