Session #1 - Hibernate introduction and creating CRUD application

Learn Hibernate ORM Framework programming though this Hibernate video tutorial. Hibernate video tutorial for creating CRUD applications.

Session #1 - Hibernate introduction and creating CRUD application

Session #1 - Video tutorial of Hibernate introduction and creating CRUD application. Example code of insert, update, search and delete data using Hibernate framework.

This video tutorial introduces you with the Hibernate framework and teaches you how to create simple CRUD application. Hibernate is one of the most used ORM framework for developing the persistence layer for Java based applications. It can be used to develop the persistence layer for web and desktop applications.

This video tutorial is for the beginners in Hibernate programming, you will learn the basics of Hibernate and then create a simple CRUD application. We have used MySQL Database for this tutorial, but you can use other database for running the example discussed here.

I am assuming that you have knowledge of JDBC programming and you are aware of database concepts. You should be able to work Eclipse IDE and well versed with the Java programming language.

Topics covered in this session are:

  • Data persistence
  • Hibernate Introduction
  • Hibernate Architecture
  • Hibernate Annotations
  • Setting up the project
  • Developing and running the Crud application

Here is the video tutorial of Hibernate:

Above video tutorial teaches you Hibernate ORM framework in great detail. You can download the source code of the application here. After download you can extract the zip file and open the project in Eclipse IDE. You can also create your own project and then just use the code files in  your project. You will need MySQL database to run the example programs.

Following image shows the agenda of this Hibernate video training session:

Following image shows the ways to persist the data.

Hibernate is popular ORM tool. ORM tool increases the productivity of the developers. It makes your application highly maintainable. Application developed in Hibernate gives very good performance. You can even add external caching library for increasing the performance of the application. ORM is database independent which makes your application portable across databases.

Hibernate Framework's components are Hibernate Connection Manager, Hibernate Transaction Manager and ORM tool.

Here is the components of Hibernate Framework:

Here is the architecture of Hibernate framework:

Full cream architecture of Hibernate Framework:

Here is the SQL code for creating table for running this application:

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (
`id` int(11) NOT NULL auto_increment,
`sname` varchar(40) NOT NULL,
`sroll` int(11) NOT NULL,
`scourse` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

Here is the code of Student.java class which is the Model object:

/**
 * 
 */
package roseindia;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import javax.persistence.Table;;

/**
 * @author Rose India
 *
 */
@Entity
@Table(name = "student")
public class Student implements Serializable{
	
		
	@Id
	@GeneratedValue
	private int id;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name="sname", nullable=false,length=40)
	private String sname;
	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}
	
	@Column(name="sroll",nullable=false)
	private int sroll;
	public int getSroll() {
		return sroll;
	}

	public void setSroll(int sroll) {
		this.sroll = sroll;
	}
	
	@Column(name="scourse",nullable=false,length=10)
	private String scourse;
	public String getScourse() {
		return scourse;
	}

	public void setScourse(String scourse) {
		this.scourse = scourse;
	}
	

}

Here is the source code of HibernateUtil.java class:

package roseindia;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	//Singleton class
	private static final SessionFactory sessionFactory;
	static{
		try{
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		}catch(Throwable th){
			System.err.println("Enitial SessionFactory creation failed"+th);
			throw new ExceptionInInitializerError(th);
		}
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}

}

Here is the code of CreateData.java file which inserts the data into database:

package roseindia;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class CreateData {


	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
		SessionFactory sessFact = HibernateUtil.getSessionFactory();
		Session sess = sessFact.getCurrentSession();
		org.hibernate.Transaction tr = sess.beginTransaction();
		
		Student stu = new Student();
		stu.setSname("Deepak 2");
		stu.setSroll(100);
		stu.setScourse("B.C.A.");
		
		sess.save(stu);//Save data
		tr.commit();
		System.out.println("Successfully inserted");
		//sess.close();
	}

}

Here is the code ReadData.java file which is used to read the data from database using Hibernate Framework:

/**
 * 
 */
package roseindia;

import java.util.Iterator;
import java.util.List;

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

/**
 * @author Deepak Kumar
 *
 */
public class ReadData {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		SessionFactory sessFact = HibernateUtil.getSessionFactory();
		Session sess = sessFact.getCurrentSession();
		Transaction tr = sess.beginTransaction();
		Query query = sess.createQuery("from Student");
		List result = query.list(); 
		Iterator it = result.iterator();
		System.out.println("id  sname  sroll  scourse where id > 10");
		while(it.hasNext()){
			Student st = (Student)it.next();
			System.out.print(st.getId());
			System.out.print("   "+st.getSname());
			System.out.print("   "+st.getSroll());
			System.out.print("   "+st.getScourse());
			System.out.println();
		}
		sessFact.close();

	}

}

Here is the code of UpdateData.java which is used to update the data:

/**
 * 
 */
package roseindia;

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

/**
 * @author Deepak Kumar
 *
 */
public class UpdateData {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		
		SessionFactory sessFact = HibernateUtil.getSessionFactory();
		Session sess = sessFact.getCurrentSession();
		Transaction tr = sess.beginTransaction();
		int i=7;
		Student st = (Student)sess.load(Student.class,i);
		st.setSname("Aman1");
		sess.update(st);
		tr.commit();
		System.out.println("Update Successfully");
		sessFact.close();

	}

}

Here is the code of DeleteData.java which teaches you how to delete the data using Hibernate Framework:

/**
 * 
 */
package roseindia;

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

/**
 * @author Deepak Kumar
 *
 */
public class DeleteData {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		 
		SessionFactory sessFact = HibernateUtil.getSessionFactory();
		Session sess = sessFact.getCurrentSession();
		Transaction tr = sess.beginTransaction();
		Student st = (Student)sess.load(Student.class,3);
		sess.delete(st);
		System.out.println("Deleted Successfully");
		tr.commit();
		sessFact.close();

	}
}

Following is the screen shot of the read data example.

0

Read more tutorials on Hibernate at our Hibernate tutorial section.

 You can download the source code of the application here.