Why do we use POJO class in Hibernate?

POJO stands for Plain Old Java Objects. A POJO class is java a bean with getter and setter methods for each property of the bean.

There are following properties of POJO class.

  • 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.
  • It will be not extend any classes, implement interface or contain specific annotations.
  • We have to declare @Entity annotation on above POJO class so this class name will be map with db table.
  • POJO also comes with a default constructor that makes it easier to persist the data in Hibernate.
  • This is a simple java class, that encapsulates an object’s properties and provides access through setters and getters.
  • We can declare any type variable with private access modifier so that we give the permission from setters and getters to which type of variable we can access or not.
				
					import javax.persistence.Entity;

@Entity
public class EmployeePojo {

	private String id;
	private String name;

	public EmployeePojo() {

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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