Share on Google+Share on Google+

Hibernate Entity

This section contains the concept of hibernate Entity.

Hibernate Entity

This section contains the concept of hibernate Entity.

Hibernate Entity: In basic terms an entity is a stand alone object that has some distinct value.
In Hibernate's persistent mechanism ,an entity is a business object, which can be managed freely by other objects.
Each entity has associated distinct table in relational database. Table row is represented by instance ot the entity.
Entity class is a simple annotated POJO.

Here are some points to be noted during writing entity class:

Class must have constructor

Create getter setter for each field

Don?t define any field static or final. 

Here is an persistence entity class named mapped the employee table.

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 implements Serializable {

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


Here is your main class -

package net.roseindia.main;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateEntityExp {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Employee emp = new Employee();
emp.setDateOfJoin(new Date());
Transaction transaction = session.beginTransaction();;
String hql = "SELECT, emp.salary, emp.dateOfJoin from Employee emp";
Query query = session.createQuery(hql);
List objectList = query.list();
Iterator iterator = objectList.iterator();
System.out.println("Employee Name \t Salary \t Date of Join");
while (iterator.hasNext()) {
Map map = (Map);
System.out.print("\t\t" + map.get("1"));
System.out.print("\t\t" + map.get("2"));



Description : Here you are inserting new record by setting value. saves the emp object into your table.

By using HQL we are printing the table record.

Here is your output -

Hibernate: insert into employee (date_of_join, name, salary) values (?, ?, ?)
Hibernate: select as col_0_0_, employee0_.salary as col_1_0_, employee0_.date_of_join as col_2_0_ from employee employee0_
Employee Name  Salary   Date of Join
Mandy          20000    2010-03-12 00:00:00.0
Som            20000    1999-06-22 00:00:00.0
Mandy          15000    2003-12-23 00:00:00.0
Roxi           220000   2001-02-03 00:00:00.0
Rowdy          20000    2012-06-27 00:00:00.0
Ron            20000    2012-06-27 00:00:00.0



Posted on: July 2, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Entity  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image