Hibernate Create POJO Classes (original) (raw)

Last Updated : 23 Jul, 2025

POJO stands for Plain Old Java Object. In simple terms, we use POJO to make a programming model for declaring object entities. The classes are simple to use and do not have any restrictions as compared to Java Beans.

To read about POJO classes and Java Bean refer to the following article - POJO classes and Java Bean

POJO is handier as its best in readability and reusability. The only difference between java bean and POJO classes is POJOs don't have any restrictions other than java but java beans have.

Properties of POJO classes

  1. The POJO classes must be public so that we can use them outside the class.
  2. POJO does not have any name convention for properties and methods. It's just a java class consisting of some variables and getters-setters.
  3. POJO classes are used in hibernate for mapping to database objects. That means all object entities we make in POJO classes will be reflected in a database object.
  4. It should not extend classes, implement interfaces, or contain prespecified annotations. Also, it does not bound with any restrictions but strictly follows java language specifications.
  5. In hibernate, POJOs also contain some annotations like @Entity, @Table, @id, etc for avoiding XML files for database objects.

Working with POJO classes

POJO classes are used to encapsulate business logic and its members are treated as a database entity. The main motive of POJO classes is to define an object entity for hibernating.

Implementation:

Let us make an employee POJO class

A. File: Employee.java

Java `

// Java Program to Illustrate Employee Class // POJO class

// Importing required classes import java.io.*;

// Class class Employee {

// Private variables which are treated as an entity
private int empid;
private String name;
private int age;

// Getters and setters

// Getter
public int getEmpid() { return empid; }

// Setter
public void setEmpid(int empid) { this.empid = empid; }

// Getter
public String getName() { return name; }

// Setter
public void setName(String name) { this.name = name; }

// Getter
public int getAge() { return age; }

// Setter
public void setAge(int age) { this.age = age; }

}

`

Let's make a POJO class method and use them to set and get the data.

B. File: MainClass.java

Java `

// Importing required classes import java.io.*;

// Main class class MainClass {

// Main driver method
public static void main(String[] args)
{

    // Making a POJO class method to
    // set and retrieve some values
    employee emp = new employee();

    // Setting the values with setters
    emp.setEmpid(123);
    emp.setName("Geek");
    emp.setAge(21);

    // Retrieving some values from getters
    System.out.println("The employee ID is "
                       + emp.getEmpid());
    System.out.println("The name of the employee is "
                       + emp.getName());
    System.out.println("The age of the employee is "
                       + emp.getAge());
}

}

`

Output: