Thursday , February 22 2018
Home / Hibernate / Example in Hibernate XML Config Example in Hibernate XML Config

In this tutorial, we are going to learn about configuration in hibernate. By defining the we can execute the ddl(Data Definition Language) commands (CREATE,ALTER,DROP AND TRUNCATE) from the hibernate framework. 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.


<property name=””>value</property>


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

  • create
  • validate
  • update
  • create-drop

Lets me explain in detail, for each property. 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=””>create</property>

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

Syntax :


<property name="">validate/create/update/create-drop</property>


<property name="" value="validate/create/update/create-drop"></property>


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

Create Pojo class :

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 :


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    <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 :


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "">
        <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="">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()
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
        SessionFactory factory = configuration.buildSessionFactory(builder

        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 : Create-Example-min 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

<property name=””>validate</property> 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=””>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. 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=””>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+ ,


Hibernate Left Join Example

In this tutorials, I am going to show you how to work with Hibernate Left …

Leave a Reply

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