hbm2ddl.auto Example in Hibernate XML Config

By |2019-04-09T20:29:16+05:30May 7th, 2016|Hibernate|

In this tutorial, we will see what is hbm2ddl.auto configuration in hibernate.

Hibernate hbm2ddl.auto:

hbm2ddl.auto is a hibernate configuration property. It is used to validate and exports schema DDL to the database when the SessionFactory is created.

If we want to make use of it, we should have to pass the appropriate values to the hibernate.cfg.xml like below.

hibernate.cfg.xml
<hibernate-configuration>
<session-factory>

<property name="hbm2ddl.auto">value</property>

</session-factory>
</hibernate-configuration>

By defining the hbm2ddl.auto property, we can execute the DDL (Data Definition Language) commands from the hibernate framework, while creating the SessionFactory itself.

The hbm2ddl.auto property of Hibernate either creates or validates a database table.

The possible values for hbm2ddl.auto:

  • create
  • validate
  • update
  • create-drop

Syntax :

hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
   <session-factory>
      <property name="hbm2ddl.auto">validate/create/update/create-drop</property>
      Or
      <property name="hbm2ddl.auto" value="validate/create/update/create-drop" />
   </session-factory>
</hibernate-configuration>

hbm2ddl.auto Create :

If the value is CREATE then the hibernate first drops the existing tables data and structure, then creates new tables and executes the operations on the newly created tables.

<property name="hbm2ddl.auto">create</property>

The only problem with the value “create” is, we lose existing table data.

hbm2ddl.auto Validate :

If the value is validate then hibernate only validates the table structure- whether the table and columns have existed or not. If the table doesn’t exist then hibernate throws an exception.

Validate is the default value for hbm2ddl.auto.

<property name="hbm2ddl.auto">validate</property>

hbm2ddl.auto update :

If the value is update then, Hibernate checks for the table and columns. If a table doesn’t exist then it creates new tables and where as if a column doesn’t exist it creates new columns for it.

<property name="hbm2ddl.auto">update</property>

But in the case of value “update” hibernate doesn’t drop any existing table, so that we don’t lose existing table data.

hbm2ddl.auto create-drop :

If the value is create-drop then, Hibernate first checks for a table and do the necessary operations and finally drops the table after all the operations were completed.

<property name="hbm2ddl.auto">create-drop</property>

The value “create-drop” is given for unit testing the hibernate code.

Here is a complete (validate,create,update,create-drop) example:

hbm2ddl.auto Create Example:

Create Pojo class : Student.java

Student.java
package com.otp.hibernate.pojo;

public class Student {
    private int studentId;
    private String studentName;
    private String address;

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

Create Hibernate Mapping File : student.hbm.xml

student.hbm.xml
<hibernate-mapping>
    <class name="com.otp.hibernate.pojo.Student" table="student">
        <id name="studentId" column="sId">
            <generator class="increment"/>
        </id>
        <property name="studentName" column="name2" />
        <property name="address" column="address" />
    </class>
</hibernate-mapping>

Hibernate configuration file: hibernate.cfg.xml

hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
       
   <session-factory>
              
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
              
      <property name="connection.url">jdbc:mysql://localhost:3306/onlinetutorialspoint</property>
              
      <property name="connection.username">root</property>
              
      <property name="connection.password">123456</property>
              
      <property name="hbm2ddl.auto">create</property>
              
      <mapping resource="student.hbm.xml" />
          
   </session-factory>
</hibernate-configuration>

Run the Application :

Main.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import com.otp.hibernate.pojo.Student;

public class Main {
  public static void main(String[] args) {
    Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties());
    SessionFactory factory = configuration.buildSessionFactory(builder.build());
    Session session = factory.openSession();
    Transaction transaction = session.beginTransaction();
    Student student = new Student();
    student.setStudentName("Chandra Shekhar");
    student.setAddress("Hyderabad");
    session.save(student);
    transaction.commit();
    session.flush();
    session.close();
    System.out.println("Transaction Completed !");
  }
}

By running the above program, we can see the database output like below. Output : <div_class=”post_pic”>hbm2ddl.auto Create-Example-min

 

Happy Learning 🙂

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment