<<< Prev

Next >>>

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


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;


4) Create the mapping file for Persistent class : 


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


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

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

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();  
		    session.persist(s1);//persisting the object  
		    t.commit();//transaction is commited  
		    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: