Hibernate Entity

This section contains the concept of hibernate Entity.


Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORMwith Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1?

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 Employee.java.It 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.io.Serializable;
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) {
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;

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.name, 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) iterator.next();
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.

session.save(emp) 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 employee0_.name 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


Share on Google+Share on Google+

Hibernate Entity

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



Discuss: Hibernate Entity  

Post your Comment

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