<<< Prev

Next >>>

Hibernate Example

Hibernate Example:

Please Follow these steps to create first hibernate application.

  1. Create a Persistent class.
  2. Create a mapping file for persistent class.
  3. Create the Configuration file.
  4. Create a class that retrieves or stores the persistent object.
  5. Load the Jar File.

Run your program as java application

Explanation of all step by programming.

  1. First create a Persistent class.-

Let’s take one student.class.

package com.qualde;
public class Student {
private int id;  
public int getId() {
return id;
public void setId(int id) { = id;
public String getFirstName() {
return firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
public String getLastName() {
return lastName;
public void setLastName(String lastName) {
this.lastName = lastName;
private String firstName,lastName;  

  1. Create another mapping class.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
  <class name="com.qualde.Student" table="stu1">  
    <id name="id">  
     <generator class="assigned"></generator>  
    <property name="firstName"></property>  
    <property name="lastName"></property>    


  1. Now Configuration file:It contain information about database and mapping file.


<?xml version="1.0"coding="UTF-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
        <property name="">update</property>  
       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stu1</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property> 
    <mapping resource="student.hbm.xml"/>  

  1. Store data.class (to store our data to database.)
package com.qualde;
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.boot.Metadata;  
import org.hibernate.boot.MetadataSources;  
import org.hibernate.boot.registry.StandardServiceRegistry;  
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
public class StoreData {  
    public static void main( String[] args )  
         StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
            Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
        SessionFactory factory = meta.getSessionFactoryBuilder().build();  
        Session session = factory.openSession();  
        Transaction t = session.beginTransaction();  
         Student s1=new Student();    
       System.out.println("successfully saved");    


  1. Load the jar file: You have to download jar file in your library.
  2. Run your project as java application.

       Note: You can run your project in eclipse, Sts etc tools.And project structure will look                   like this