Open In App

Automatic Table Creation Using Hibernate

Hibernate is a Java framework that implements ORM(Object Relational Mapping) design pattern. It is used to map java objects into a relational database. It internally uses JDBC(Java Database Connectivity), JTA(Java Transaction API), and JNDI(Java Naming and Directory Interface). It helps to make java objects persist in the database without losing their state, thus, named Hibernate. It can be used to perform all the CRUD operations without having to write SQL queries. Hibernate framework can be used to create tables in the database automatically. Below property is added in the configuration file to create tables automatically.

<property name=”hibernate.hbm2ddl.auto”>create</property>



“hibernate.hbm2ddl.auto” property accepts the following values:

Below is an example demonstrating the automatic table creation in hibernate using the MySQL database.



Example Project

hibernate.cfg.xml file:




<?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>

Student.hbm.xml file:




<?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>

Student.java file:




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;
    }
  
}

Test.java file:




package p1;
  
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
  
import beans.Student;
  
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);
            Student s=new Student();
            s.setId(101);
            s.setName("Raghav");
            session.save(s);
            Transaction t=session.beginTransaction();
            t.commit();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Output:

A new table ‘student’ is created and data from the Student class object is mapped into the table.

 


Article Tags :