top of page

What is Hibernate : Java Programming Help

Updated: Mar 23, 2021

Hibernate is one of the best ORM framework available for java


ORM : Object Relation Mapping


If you think about this software world. WE HAVE SOFTWARES and data . The main thing is data . May be we want to access,save,modify to do that we write an application. That application can be written in any lang : C#, java, Python and if you work with the lang .and you want to connect them with the help of the database then we need connector that can be implemented with the help of JDBC So you as a programmer you have to write the application if you are using java then you have to thing in terms of object. And in other hand we have table is database and we write sql queries. What happen if you don’t have to write SQL queries that where the ORM framework is famous so based on your classes based on your how many object you have created it will define your table and data.


Prerequisites of Hibernate :


You should be know 1 thing

1: java

2: Sql Concepts


What is Hibernate : Java Programming Help
What is Hibernate : Java Programming Help

We have to know how to connect them


Hibernate theory:


Hibernate is ORM tools to persist the data.


Let me give the example : like we are living the real world and we are dealing with the real life software and we can build the software with the help of any lang. one of the lang we use to develop is java.when you make any software then that software is working on data.


Now the data can be any variable(int,float) any objects(Students, Employee)


Suppose we are creating 1 class


Class Student{

Int id;

String name;

}



And when we create any object of this class then it will be having two values and I wanna make sure that those value get stored somewhere(Mysql,Oracle) Concept of store the data into tha database it s persistent . Varaible in the class is transient e normally we don’t store the data into the databse .we use JDBC to connect our application to the database.

But java developer is not comfortable with sql lang to work on databse we must know the sql lang . so here solution is that Hibernate.


How to develop project for hibernate or maven


1 ) By the help of maven you can create open eclipse->new > maven project->internal(select quick start maven1.1) then give name of your project and then next


2 ) Now open any browser type mvnrepositry.com


Type over there hibernate and search for it and now copy the dependencies and then paste into the porm.xml file and now you have 2 use mysql connector as well.

And 1 more thing to work in hibernate there is configuration file of hibernate is also use so for that you have to install 1 software


Go to eclipse marketplace and now type there hibernate and you will see


Now your project sturcutre is ready;


Now create 1 Pojo class I have created student class


Student.java



package com.codersarts.DemoHib;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
    @Id
    private String id;
   private String name;
   private String contact;

   public String getId() {
           return id;
  }

      public void setId(String id) {
                           this.id = id;
	}


    public String getName() {
            return name;
	}

   public void setName(String name) {
           this.name = name;
   }

     public String getContact() {
              return contact;
	}

       public void setContact(String contact) {
          this.contact = contact;
	}
}



App.java



package com.codersarts.DemoHib;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* Hello world!
*
*/
public class App  
{
   public static void main( String[] args )
   {
   	Student stu1 = new Student();
   	stu1.setId("01");
   	stu1.setName("Ankit");
   	stu1.setContact("8247666541");
   	Configuration con = new Configuration().configure().addAnnotatedClass(Student.class);
   	ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry();
		SessionFactory sf = con.buildSessionFactory(reg);
   	Session session = sf.openSession();
   	Transaction tx = session.beginTransaction();
   	session.save(stu1);
   	tx.commit();
   }
}




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">org.postgresql.Driver</property>
<property name="hibernate.connection.password">coder</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/customer</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>



We should use here update because whn we use ceate here then every time new Table will created and prvious one will be deleted

And when we use update the it first check if the table is exist or not if not then it created first time after that it updated in that table.


How to configure hibernate.cfg.xml file?


Here I am using postgres SQL

So I am providing detail so postgres like that.


Right click on project and go to on the and the hibernate


click here to download the pdf file here


bottom of page