Share on Google+Share on Google+

ratna rathor
Explain the persistence class in hibernate?
2 Answer(s)      4 years and 10 months ago
Posted in : Hibernate Interview Questions

What is persistence class in hibernate?


View Answers

June 8, 2012 at 6:49 PM

Persistence class are simple POJO classes in an application. It works as implementation of the business application for example Employee , department etc. It is not necessary that all instances of persistence class are defined persistence.


package net.roseindia.table;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

@Table(name = "employee")
public class Employee {
    @Column(name = "emp_id")
    private int id;

    @Column(name = "name")
    private String name;

    @Column(name = "salary")
    private int salary;

    @Column(name = "date_of_join")
    private Date dateOfJoin;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public int getSalary() {
        return salary;

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

    public Date getDateOfJoin() {
        return dateOfJoin;

    public void setDateOfJoin(Date dateOfJoin) {
        this.dateOfJoin = dateOfJoin;


Description: The javax.persistence package contains the classes and interfaces that is used to define the relationship between persistence provider and the managed classes the client of java persistence API.

June 9, 2012 at 2:42 AM

The entire concept of Hibernate is to take the values from Java class attributes and persist them to a database table. A mapping document helps Hibernate in determining how to pull the values from the classes and map them with table and associated fields.

Java classes whose objects or instances will be stored in database tables are called persistent classes in Hibernate. Hibernate works best if these classes follow some simple rules, also known as the Plain Old Java Object (POJO) programming model. There are following main rules of persistent classes, however, none of these rules are hard requirements.

All Java classes that will be persisted need a default constructor.

All classes should contain an ID in order to allow easy identification of your objects within Hibernate and the database. This property maps to the primary key column of a database table.

All attributes that will be persisted should be declared private and have getXXX and setXXX methods defined in the JavaBean style.

A central feature of Hibernate, proxies, depends upon the persistent class being either non-final, or the implementation of an interface that declares all public methods.

All classes that do not extend or implement some specialized classes and interfaces required by the EJB framework.

The POJO name is used to emphasize that a given object is an ordinary Java Object, not a special object, and in particular not an Enterprise JavaBean.

Based on the few rules mentioned above we can define a POJO class as follows:


Advertisement null