Pages Navigation Menu

Coding is much easier than you think

Hibernate 4 Hello World example in Eclipse using Annotation


 
This is the 4th article on Hibernate in java application that describes on how to to save an object from java into the database using Hibernate 4(Hibernate 4 Insert Query). If you have not read my previous articles article on Generic Hibernate Application Requirements and Steps to be followed to use Hibernate in Java, I will recommend that you read that article first. You may want to look at Hibernate Installation/Setup on Eclipse IDE article if Hibernate is not installed already on your system.
 
As described earlier, the following files are the minimum requirement to shape an hibernate program..
 
StudentEntity.java (Entity class)
hibernate.cfg.xml (Xml configuration file)
HibernateUtil.java (Main class to access this configuration file and to write hibernate logic)
 

Project Structure

 
The final appearance of the application should be as follows:
 

 

Entity class

 

package entity;

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

@Entity
@Table(name = "STUDENT")
public class StudentEntity {

	@Id
	@Column(name = "ID")
	private int id;
	
	@Column(name = "NAME")
	private String name;
	
	@Column(name = "DEPARTMENT")
	private String department;
	
	@Column(name = "COLLEGE")
	private String college;

// Create Getters and Setters
}

 
Note: I have explained about every annotations used in the above file in the article Generic Hibernate Application Requirements
 

Hibernate Configuration file

 
Create a new XML file and give this new configuration file the default name hibernate.cfg.xmland place it src directory of your project.
 
File: hibernate.cfg.xml






	
	
                  oracle.jdbc.driver.OracleDriver
        
	system
	admin
	
                   jdbc:oracle:thin:@xxx.x.x.x:1521:XE
        

	
	org.hibernate.dialect.Oracle10gDialect

	
	true

	
	update
	
    



 

Hibernate Utility

 
Create the Main class to run the example.
 

package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import entity.StudentEntity;

public class HibernateUtil {

	public static void main(String[] args) {

        Configuration cf = new Configuration().configure("hibernate.cfg.xml");

        StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();
        srb.applySettings(cf.getProperties());
        ServiceRegistry sr = srb.build();
        SessionFactory sf = cf.buildSessionFactory(sr);

        Session session = sf.openSession();
    
        StudentEntity std = new StudentEntity();
		std.setId(24); // Primary Key
		std.setName("Jamil");
		std.setDepartment("ECE");
		std.setCollege("SKCET");

		Transaction tx = session.beginTransaction();
		session.save(std);
		tx.commit();
		System.out.println("Object saved successfully.....!!");
		session.close();
		sf.close();
	}
}

 
In the article Steps to be followed to use Hibernate in Java, I have explained the configuration/code used in above program in detail.
 
Now once our project is ready. Right click to project or right click to HibernateUtil.java and click Run As–>Java Application. Since I have set show_sqlto truein hibernate.cfg.xml, so the hibernate create and insert quires will be displayed on eclipse console as below.
 

 
You will see the data stored in Student table in the database.
 

 
In my next article I have implemented Select Query in Hibernate
 

One Comment

  1. Very excellent ! I’ve got a problem it works very well with v4.3 but not with 5.3 et 4.2, i dont know why. i’ve got a Unknown entity: entity.StudentEntity error. Thank you anyway !