Monday , February 27 2017
Home / Hibernate / hbm2ddl.auto Example in Hibernate XML Config

hbm2ddl.auto Example in Hibernate XML Config

In this tutorial, we are going to learn about hbm2ddl.auto configuration in hibernate. By defining the hbm2ddl.auto we can execute the ddl(Data Definition Language) commands (CREATE,ALTER,DROP AND TRUNCATE) from the hibernate framework.

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

<hibernate-configuration>
<session-factory>

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

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

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

Lets me explain in detail, for each property.

hbm2ddl.auto Create :

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

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

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

Syntax :

<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"></property>

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

Here is the complete all(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


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

Create hibernate 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>
        <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 :

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 :

hbm2ddl.auto Create-Example-min

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. So we don’t loose existing data of a table.

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.

 

Download Example

About chandrashekhar

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+ ,

Check Also

Hibernate Projection with Example

In this tutorials, we are going to learn about Hibernate Projection with a simple example. …

Leave a Reply

Your email address will not be published. Required fields are marked *