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.

Ads

Tutorials   
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 ORM with 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 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 ORM 4.3.0.Final released 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
Ads

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.

Read more tutorials on Hibernate at our Hibernate tutorial section.

 You can download the source code of the application here.

Advertisements

Ads
Share on Google+Share on Google+

Session #1 - Hibernate introduction and creating CRUD application

Posted on: October 22, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Session #1 - Hibernate introduction and creating CRUD application  

Post your Comment


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

Ads

 

Ads