Hibernate hbm2ddl property

By | July 20, 2018

In our previous post Why to use hibernate dialect? we understood all the concept related to hibernate dialect property

In this post we will talk about hibernate hbm2ddl property in detail using an example.

Hibernate hbm2ddl property you can configure in your hibernate.cfg.xml as below

The possible values for hibernate.hbm2ddl.auto are:

  • create
  • validate
  • update
  • create-drop

Create

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

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

Let’s modify hibernate.cfg.xml for hibernate.hbm2ddl.auto property as below

Now login to your MYSQL database console and connect with database schema(test)

After Running ClientTest.java with createEmployee(employeeService) (Demo project) method.

Output of Eclipse console and MySqlWorkbench are as below in this case

 

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.

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.

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

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.

The value “create-drop” is given for unit testing/POC kind of functionalities  in the hibernate.

Now let’s have a practical  example to understand how  hibernate hbm2ddl property works with different options(create,validate,update,create-drop)

To proceed with this demo we will refer our Hibernate CRUD(Create,Read,Update and Delete) example

That’s all about Hibernate hbm2ddl property

You May Also Like:

Latest hibernate distribution Zip file download link
Hibernate 5 distribution binary details
Create SessionFactory in Hibernate5 using hibernate.cfg.xml
Create SessionFactory in Hibernate5 without hibernate.cfg.xml
Save and persist an entity example in hibernate
Hibernate CRUD(Create,Read,Update and Delete) example
Dirty checking in hibernate example
Understanding hibernate Configuration File
Why to use hibernate dialect?
What are the benefits of using hibernate?

If you have any feedback or suggestion please feel free to drop in below comment box.

 

Leave a Reply

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