Hibernate Persistent Classes

Persistent classes are the classes in an application whose objects or instances can be stored in database.

The term "persistent" here means that the classes are able to be persisted, not that they are in the persistent state.

Hibernate works best if these classes follow some simple rules, also known as the Plain Old Java Object (POJO) programming model. However, none of these rules are hard requirements.

Simple Pojo\Entity:

@Entity
@Table(name="Employee")
public class Employee {
@Id
private int id;
@Column(name="NAME")
private String name;
@Column(name="SALARY")
private int salary;

public Employee() {
}

public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}

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 int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}
}

1) Implement a no-argument constructor: All persistent classes must have a default constructor (which can be non-public) so that Hibernate can instantiate them using java.lang.reflect.Constructor.newInstance().

2) identifier property Class should have a property named id.

3) getter setter All attributes that will be persisted should be declared private and have getXXX and setXXX methods.

4) Prefer non-final classes A central feature of Hibernate, proxies (lazy loading), depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.