Persistent classes are those java classes whose objects have to be stored in the database tables. They should follow some simple rules of Plain Old Java Object programming model (POJO).
Some rules that should be, not must be followed by a persistence class.
- A persistence class should have a default constructor.
- A persistence class should have an id to uniquely identify the class objects.
- All attributes should be declared private.
- Public getter and setter methods should be defined to access the class attributes.
Example:
Student.java
/** * This class represents a persistent class for Student. * @author w3schools */ public class Student { //data members private int studentId; private String firstName; private String lastName; private String className; private String rollNo; private int age; //no-argument constructor public Student(){ } //getter and setter methods public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getRollNo() { return rollNo; } public void setRollNo(String rollNo) { this.rollNo = rollNo; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
Next Topic: Object states in Hibernate with example.
Previous Topic: Hibernate configuration file with example.