Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHibernate Example without IDE

Hibernate Example without IDE

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. 

ORM

 

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:

hibernate.cfg.xml:

XML




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate Configuration DTD 3.0//EN" 
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.hbm2ddl.auto">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>
    </session-factory>
</hibernate-configuration>


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:

Student.java:

Java




package p1;
  
public class Student {
    
    private int id;
    private String name;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.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

Student.hbm.xml:

Java




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


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

Test.java

Java




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) {
        try
        {
            Configuration config=new Configuration();
            config.configure();
            System.out.println(config);
            SessionFactory sessionFactory=config.buildSessionFactory();
            Session session=sessionFactory.openSession();
            System.out.println(session);
            
              // creating Student class object
            Student s=new Student();
            s.setId(13773);
            s.setName("Naman");
            
              // saving object of Student
              // class in the session cache
            session.save(s);
            Transaction t=session.beginTransaction();
            t.commit();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}


Save Student.java and Test.java 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

RELATED ARTICLES

Most Popular

Recent Comments