What is Persistent classes in Hibernate?

In the previous article, we have seen the How to Handle session Object in Hibernate you can check it here How to handle session in Hibernate. In this article, we will discuss What is Persistent Class in Hibernate.

As we know, Hibernate is a framework that maps the data from the POJO class to the database. So, We will cover what is a persistent class and with a simple example.

What is Persistent Class?

The persistent class is a class in Java application that is created to implement the objects of business problems. The persistent class follows the POJO model(Plain Old Java Object).

  • Persistent class doesn’t have special restrictions.
  • The persistent class encapsulates the Business Logic.

ORM(Object Relational Mapping) responsibility is to take the values from the POJO class and saves its data into the database.

What are the Elements in Persistent Class?

The POJO class or the persistent class in Hibernate should include the following elements

  • It should contain the default constructor.
PoJoClass()
{
//default constartuctor
}
  • Every class should have an id that helps us to identify each element uniquely.
private int id; // for primary key
  • Every attribute should be private.
private int id;
private String attribute1;
private String attribute2;
....
....
....
private String attributeN;
  • Every attribute should have a getter and setter method.
<--------SETTER METHOD--------->
public void setattribute1(String a)
{
this.attribute=a; 
}
<--------GETTER METHOD--------->
public String getattribute()
{
return attribute;
}
  • Always prefer non-final classes which is the main feature of Hibernate.
  • Implement equals() and hashcode() method in the persistent class.

Example of Persistent class in Hibernate

In this example, We will see how to create a Cricketer POJO class and two subclass Bowler and Batsman.

Cricketer.java

public class Cricketer {
  private int id;
  private String name;
  private String birthday;
  private String totolrun;
  private String totalwicket;

  public Cricketer() {
    super();
    // TODO Auto-generated constructor stub
  }

  public int getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

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

  public String getBirthday() {
    return birthday;
  }

  public void setBirthday(String birthday) {
    this.birthday = birthday;
  }

  public String getTotolrun() {
    return totolrun;
  }

  public void setTotolrun(String totolrun) {
    this.totolrun = totolrun;
  }

  public String getTotalwicket() {
    return totalwicket;
  }

  public void setTotalwicket(String totalwicket) {
    this.totalwicket = totalwicket;
  }

}

Bowler.java

public class Bowler extends Cricketer {

  private String name;

  public String getName() {
    return name;
  }

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

}

Batsman.java

public class Batsman extends Cricketer {
  private String name;

  public String getName() {
    return name;
  }

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

Thus this is How we create a POJO or Persistent class in Hibernate.

In the next article of this tutorial, we will see How Hibernate maps this POJO class to a database with a simple example.