Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

hibernate entity

In this section, you will learn about entity in Hibernate.


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 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example 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 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code

hibernate entity

In this section, you will learn about entity in Hibernate.

What is entity ?

In a persistent storage mechanism, an entity is a business object. Example of entities/business objects are employee, department etc. In simplified words, a relational database is known as persistent storage mechanism. Each entity has a associated table in a rational database, each instance of the entity represents a row in this table.

Rules of writing entity class :

Since entity class is an annotated  POJO(Plain old java object), during writing the following points must be keep in mind :

  • must contain constructor.

  • should have getter setter for each field.

  • should not have static or final fields.


In the below example, you will learn about how you can implement entity in hibernate :

Database Table: student

CREATE TABLE `student` ( 
	`id` int(11) NOT NULL auto_increment, 
	`sname` varchar(100) NOT NULL, 
	`sroll` int(11) NOT NULL, 
	`scourse` varchar(10) NOT NULL, 
	PRIMARY KEY (`id`) 

Model Java Class :

package roseindia;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;


public class Student {

	private int id;
	* @return the id
	public int getId() {
	return id;
	* @param id the id to set
	public void setId(int id) { = id;
	@Column(name="sname", length=100,nullable=false)
	private String sname;
	* @return the sname
	public String getSname() {
	return sname;
	* @param sname the sname to set
	public void setSname(String sname) {
		this.sname = sname;
	private int sroll;
	* @return the sroll
	public int getSroll() {
		return sroll;
	* @param sroll the sroll to set
	public void setSroll(int sroll) {
	this.sroll = sroll;
	private String scourse;
	* @return the scourse
	public String getScourse() {
		return scourse;
	* @param scourse the scourse to set
	public void setScourse(String scourse) {
		this.scourse = scourse;


Main Class:

package roseindia;

import java.util.Iterator;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.EntityTransaction;

import javax.persistence.Persistence;

import javax.persistence.Query;

public class JPASetParameterNamedParameter {

	public static void main(String[] args) {
	// TODO Auto-generated method stub
	EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa");
	EntityManager em=emf.createEntityManager();
	EntityTransaction entr=em.getTransaction();
	Query query=em.createQuery("SELECT st FROM Student st WHERE st.sroll > :roll");
	query.setParameter("roll", 100);
	List results = query.getResultList();
	Iterator stIterator=results.iterator();
	Student st=(Student);
	System.out.print(" sroll:"+st.getSroll());
	System.out.print(" scourse:"+st.getScourse());
	System.out.println("Record not found.");



log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select as id0_, student0_.scourse as scourse0_, student0_.sname as sname0_, student0_.sroll as sroll0_ from student student0_ where student0_.sroll>?

sname:Arindam sroll:101 scourse:MCA

sname:Ajay sroll:102 scourse:B.Tech.

sname:Rakesh sroll:103 scourse:B.Tech

sname:Vikas sroll:104 scourse:BCA


Share on Google+Share on Google+

hibernate entity

Posted on: May 31, 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