Alphalearningschool
        

<<< Prev

Next >>>

Up
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) {
this.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.

          Student.hbm.xml

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

 

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

          hibernate.cfg.xml

<?xml version="1.0"coding="UTF-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">  
  
<hibernate-configuration>  
    <session-factory>  
        <property name="hbm2ddl.auto">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"/>  
    </session-factory>  
  
</hibernate-configuration>

  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();    
            s1.setId(1);    
            s1.setFirstName("Manish");    
            s1.setLastName("Kumar");    
         
       session.save(s1);  
       t.commit();  
       System.out.println("successfully saved");    
        factory.close();  
        session.close();     
    }  
}  

 

  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