2.Hibernate Example – Step by Step

Hibernate Example – Step by Step
We need
Table: student
Id         |name |address            |
1              |satya   |HYD                     |
2              |Jon       |VZG                     |

1. Simple POJO class (StudentBo)
2. Main Class (StudentDAo)
3. Mapping file (student.hbm.xml)
4. Configuration file (hibernate.cfg.xml)

1.       Simple POJO class (StudentBo)
·         It contains contains table column names as data members
·         In above table id,name,addrees are columns
·         It contains column names with setters and getters
package bo;

public class StudentBo {
  
   private int id;
   private String nameaddress;
   public int getId() {
          return id;
   }
   public void setId(int id) {
          this.id = id;
   }
    
   public String getAddress() {
          return address;
   }
   public void setAddress(String address) {
          this.address = address;
   }
   public String getName() {
          return name;
   }
   public void setName(String name) {
          this.name = name;
   }

}

2.       Main Class (StudentDAo)
·         Configuration à it will loads the all the configurations from config file and save in config object
·         Session Factory à It will connect to the database
·         Session                 à it opens the session for DB operations
·         Transaction         à to save the Changes in DB
package dao;

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

import bo.StudentBo;

public class StudentDao {
public static void main(String[] args) {
  
   System.out.println("**************** HIBERNATE ****************************");
  
   Configuration cfg = new Configuration();
   cfg.configure("res/hibernate.cfg.xml"); 
  
   SessionFactory factory = cfg.buildSessionFactory();
   Session session = factory.openSession();
   Transaction transaction = session.beginTransaction();
  
   StudentBo bo = new StudentBo();
   bo.setId(12);
   bo.setName("JANA");
   bo.setAddress("VZG");
  
   session.save(bo);
  
   transaction.commit();     
   session.close();
   factory.close();    
}
}

3.       Mapping 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 package="bo">
   <class name="StudentBo" table="student">
          <id name="id" column="id">
                 <generator class="assigned" />
          </id>
          <property name="name" column="name" index="1" type="java.lang.String" />
          <property name="address" column="address" index="2"    type="java.lang.String" />
   </class>
</hibernate-mapping>

4.       Configuration file (hibernate.cfg.xml)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>        
        <property name="connection.pool_size">1</property>
        
        <!-- Hibernate Properteies -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
          <property name="hbm2ddl.auto">validate</property>  
      

        <mapping resource="res/student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Here
hbm2ddl.auto
·         Create - drop+create
·         Update - alter+create
·         Validate - check schema
·         create-drop - drop+create+drop

hbm.cfg.xml
·         dialect
·         mapping
·         show_sql
·         format_sql

3 comments

Hibernate and spring are the frameworks of Java. A java developer should be well aware of these frameworks in order to master the technology and work efficeiently.
spring training in chennai | hibernate training in chennai
FITA Academy reviews

Java is the most robust secured and multi threaded programming language which is the reason why most the the developers go for java. A single java code can be used for various platforms.
JAVA training in chennai | java training institutes in chennai | FITA Academy Chennai

Thankful to you for sharing this heavenly blog. It will upgrade my understanding about JAVA frameworks. This blog will get some answers concerning JAVA thoughts. Similarly, it will upgrade my programming capacity.
Spring Training in Chennai | Spring Course in Chennai | Spring Hibernate Training in Chennai