Hibernate Application in Eclipse IDE

Basic Hibernate Application :

In this article, we are going to create a simple example of hibernate application using eclipse IDE.After finishing of this article You will learn how to create and run the Hibernate based projects in Eclipse IDE, via XML mapping file (hbm).

Tools and Technologies :

  • JDK 1.6
  • Hibernate 3.6.3.Final
  • Eclipse
  • MySQL 5.0.

For developing first hibernate application in Eclipse, we need to follow bellow steps:

Step 1 : Create the java project :

Open Eclipse IDE,click on File-> New -> project -> Java and select java project.Now specify the project name as FirstHibernateEx then click on next -> finish .

Basic Hibernate Application :

In this article, we are going to create a simple example of hibernate application using eclipse IDE.After finishing of this article You will learn how to create and run the Hibernate based projects in Eclipse IDE, via XML mapping file (hbm).

Tools and Technologies :

  • JDK 1.6
  • Hibernate 3.6.3.Final
  • Eclipse
  • MySQL 5.0.

For developing first hibernate application in Eclipse, we need to follow bellow steps:

Step 1 : Create the java project :

Open Eclipse IDE,click on File-> New -> project -> Java and select java project.Now specify the project name as FirstHibernateEx then click on next -> finish .


Hibernate Example in Eclipse





Step 2 : Add Jar files for hibernate and mysql :

First we need to create one folder in the project, so that we can place all required jar files in one place.

Right Click on your Project and select New -> Folder as shown below

Hibernate Example in Eclipse

In New Folder screen provide Folder name as lib(Dont confuse with lib we can assign any name to the folder) and Click Finish as shown below .

Hibernate Example in Eclipse

Download the latest hibernate jar files from Here. After downloading the Jar files copy following Jar files inside the lib folder which we created earlier.

  • antlr-2.7.6
  • commons-collections-3.1
  • dom4j-1.6.1
  • hibernate3
  • javassist-3.4.GA
  • jta-1.1
  • slf4j-api-1.5.6
  • slf4j-simple-1.5.6
  • mysql

Now add these jar files to classpath of your project.

Step 3 : Create the persistence class :

Create a package by Right Click on your Project and navigate to New -> Package and provide the name com.jwt.hibernate to the package and click on Finish as shown bellow.

Hibernate Example in Eclipse

Now create a java class Student.java in this package by right click on the package -> New -> Class and add following code in this class.

Student.java
package com.jwt.hibernate;

public class Student {
private long id;
private String name;
private String degree;
private String roll;
private String phone;

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public String getRoll() {
return roll;
}
public void setRoll(String roll) {
this.roll = roll;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}

Step 4 : Create the mapping file for Persistent class :

Right click on your package then navigate to New -> File and provide the name as student.hbm.xml and click Finish.

Hibernate Example in Eclipse

Add bellow code into student.hbm.xml file

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.jwt.hibernate.Student" table="STUDENT">
<id column="ID" name="id" type="long" />
<property column="STUDENT_NAME" name="name" type="string" />
<property column="DEGREE" name="degree" type="string" />
<property column="ROLL" name="roll" type="string" />
<property column="PHONE" name="phone" type="string" />
</class>
</hibernate-mapping>

In this mapping file, Student class is linked with STUDENT table in the database, and next is the id element, means in the database table what column we need to take as primary key column, that property name we need to give here,I have been given my property name id which will mapped with ID column in the table, name is mapped with STUDENT_NAME column ,degree is mapped with DEGREE column of the STUDENT table and so on.

Note :-If your database column name and persistence class variable is same then no need to specify column property in .hbm file.



Step 5 : Create the Configuration file :

The configuration file contains informations about the database and mapping file. Conventionally, its name should be hibernate.cfg.xml .Configuration file must be in classpath of your Project.Place this file in src of your project by default it will added to classpath of your project.

hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javawebtutor</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mukesh</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create </property>
<mapping resource="com/jwt/hibernate/student.hbm.xml" />
</session-factory>
</hibernate-configuration>

Note :- In the above configuration file we mentioned <property name="hbm2ddl.auto">create </property>.If we are assigning this value as create,it will create table in database automatically when we run the application.So no need to write sql query for table creation.But when you are running the example second times you need to change this attribute to "update" otherwise hibernate will throw exception.

Step 6: Create the Test class that stores the object :

In this class, we are simply storing the student object to the database.

SimpleTest.java
package com.jwt.hibernate;

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

public class SimpleTest {

	public static void main(String[] args) {

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

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		Student student = new Student();
		student.setName("Mukesh");
		student.setRoll("101");
		student.setPhone("8888");
		student.setDegree("B.E");

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

Directory structure of the project :


Hibernate Example in Eclipse

Run the Application :

To run the hibernate application, right click on the SimpleTest class - Run As - Java Application.

Output in Eclipse :


Hibernate Example in Eclipse

In The Database :


Hibernate Example in Eclipse

You can download the source code of the example by clicking on the Download link below.

Source + Lib : Download




comments powered by Disqus