Persistent class

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.

  1. 1. A persistence class should have a default constructor.
  2. 2. A persistence class should have an id to uniquely identify the class objects.
  3. 3. All attributes should be declared private.
  4. 4. 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 javawithease
*/

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;
}
}

No comments: