Alphalearningschool
        

<<< Prev

Next >>>

Up
Hibernate Application in MyEclipse

To create Hibernate Application in my eclipse:

Please follow these steps:

  1. Create a java project.
  2. Add hibernate jars.
  3. Create Persistent class.
  4. Create mapping file and persistent class.
  5. Add mapping of hbm file in configuration file.
  6. Create a class that retrieve and stores the persistent object.
  7. Add necessary jars for mysql.
  8. Finally run the application.

Now explanation and practical of above steps:

1.Create the java project.-Go to file menu select new->Project->Java Project.(Name your java project.)

2.Add Hibernate jars and necessary jars like my sql connector jar. -Go to your project ->Right click -> Select buildpath->Add external jars.(Select all Hibernate Jars)also my sql connector jar if you are using mysql database.

3) Create the Persistent class

    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;
}

 

4) Create the mapping file for Persistent class : 

    student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.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>

 

 5. Add mapping of hbm file in configuration 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>

6. Create a class that retrieve and stores the persistent object.

      StoreData.java

package com.qualde;

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

public class StoreData {

  
		public static void main(String[] args) {  
		      
		    //creating configuration object  
		    Configuration cfg=new Configuration();  
		    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
		      
		    //creating seession factory object  
		    SessionFactory factory=cfg.buildSessionFactory();  
		      
		    //creating session object  
		    Session session=factory.openSession();  
		      
		    //creating transaction object  
		    Transaction t=session.beginTransaction();  
		          
		    Student s1=new Student();  
		    s1.setId(2);  
		    s1.setFirstName("Manish");  
		    s1.setLastName("Kumar");  
		      
		    session.persist(s1);//persisting the object  
		      
		    t.commit();//transaction is commited  
		    session.close();  
		      
		    System.out.println("successfully saved");  
		      
		}  }
		

 

7. Now run the project as java application. (right click on StoreData.class and run)

    Project structure will look like this: