hbm2ddl.auto Example in Hibernate XML Config

By | 2018-04-06T21:45:39+00:00 May 7th, 2016|Hibernate|0 Comments

In this tutorial, we will see what is hbm2ddl.auto configuration in hibernate. Actually, hbm2ddl.auto is a property which is given by hibernate.

hbm2ddl.auto is a hibernate configuration property. If we want to make use of it, we need to pass the appropriate values to it and configure the same in hibernate.cfg.xml like below.

Hibernate Doc Says: hibernate.hbm2ddl.auto, automatically validates or exports schema DDL to the database when the SessionFactory is created.

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


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


The hbm2ddl.auto property of hibernate either creates or validates a database table. The possible values for hbm2ddl.auto are:

  • create
  • validate
  • update
  • create-drop

Syntax :

<?xml version="1.0" encoding="UTF-8"?>
      <property name="hbm2ddl.auto">validate/create/update/create-drop</property>
      <property name="hbm2ddl.auto" value="validate/create/update/create-drop" />

hbm2ddl.auto Create :

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

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

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

hbm2ddl.auto Validate :

If the value is validate then hibernate only validates whether the table and columns are exist 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 table doesn’t exist then it creates a new table and if a column doesn’t exist it creates new column for it.

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

But in the case of value “update” hibernate doesn’t drop any existing table, here we doesn’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 are completed.

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

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

Here is the complete (validate,create,update,create-drop) examples :

hbm2ddl.auto Create Example:

Create Pojo class : 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

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

Create hibernate configuration file : hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
      <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" />

Run the Application :

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()
    SessionFactory factory = configuration.buildSessionFactory(builder.build());
    Session session = factory.openSession();
    Transaction transaction = session.beginTransaction();
    Student student = new Student();
    student.setStudentName("Chandra Shekhar");
    System.out.println("Transaction Completed !");

By running the above program we can see the database output like below. Output : 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