Open In App
Related Articles

Hibernate Example without IDE

Like Article
Save Article
Report issue

Hibernate is a powerful tool used to build applications that need to interact with a database. It is a Java framework that implements the ORM(Object Relational Mapping) technique. 

What is ORM?

ORM stands for Object Relational Mapping. It is a technique that is used to make Java objects persistent by mapping them into a relational database. It simplifies the interaction of an application with the database. 



Hibernate internally uses JDBC(Java Database Connectivity), JTA(Java Transaction API), and JNDI(Java Naming and Directory Interface). It can be used to perform all the CRUD operations on the database. Given below is an example demonstrating the use of Hibernate without using an ide.

Step by Step Implementation

First, download the jar files for Hibernate and add them to the classpath. You can download the files from this link. Save all the required files(explained below) in a separate folder. Hibernate requires a configuration file that contains the information about the database used in the application, mapping resources, etc. It is an XML file usually saved as hibernate.cfg.xml. Below is the sample for Hibernate configuration file:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate Configuration DTD 3.0//EN" 
        <property name="">create</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <mapping resource="Student.hbm.xml"></mapping>


Create the POJO class for the objects to be mapped into the database. POJO class stands for Plain Old Java Object class which represents the class of objects that are needed to be stored in the database. Given below is an example of the Student class:


package p1;
public class Student {
    private int id;
    private String name;
    public int getId() {
        return id;
    public void setId(int id) { = id;
    public String getName() {
        return name;
    public void setName(String name) { = name;


Create the Hibernate mapping file for the Student class. It contains important mapping information for the POJO class that needs to be mapped into the database like primary key, table name, column names, various constraints to be applied on the attributes, etc. Below is the Hibernate mapping file for the Student class



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate Mapping DTD 3.0//EN" 
    <class name="p1.Student" table="Student">
    <id name="id"></id>
    <property name="name"></property>


Now we can create a Test class that uses Hibernate to communicate with the database.


package p1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
    public static void main(String[] args) {
            Configuration config=new Configuration();
            SessionFactory sessionFactory=config.buildSessionFactory();
            Session session=sessionFactory.openSession();
              // creating Student class object
            Student s=new Student();
              // saving object of Student
              // class in the session cache
            Transaction t=session.beginTransaction();
        catch(Exception e)


Save and in package p1. Execute the following commands on the terminal to compile the files:

Compilation of Student and Test classes

Compilation of Student and Test classes

Now execute the Test class using the following command:

Execution of Test class

Execution of Test class

The following details will be stored in the database:

Student Table

Student Table

Last Updated : 17 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads