Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Hibernate Hibernate Entity

 
 

Share on Google+Share on Google+

Hibernate Entity

Advertisement
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. 

Example:
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;

@Entity
@Table(name = "employee")
public class Employee implements Serializable {

public Employee() {

}

@Id
@GeneratedValue
@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.setName("Ron");
emp.setSalary(20000);
emp.setDateOfJoin(new Date());
Transaction transaction = session.beginTransaction();
session.save(emp);
transaction.commit();
String hql = "SELECT emp.name, emp.salary, emp.dateOfJoin from Employee emp";
Query query = session.createQuery(hql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
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(map.get("0"));
System.out.print("\t\t" + map.get("1"));
System.out.print("\t\t" + map.get("2"));
System.out.println();
}

session.close();
}

}

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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: July 2, 2012

Related Tutorials

Discuss: Hibernate Entity  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com