How To Use Hibernate Without Automatic Table Creation

Posted By : Prakhar Verma | 28-Nov-2017

Hibernate is an open source, lightweight and ORM (Object Relational Mapping) tool for simplifies the development process of java application to interact with database.

Hibernate provides a feature which is useful for the developers.

Automatic table creation: Hibernate framework provides the facility to create tables of the database automatically. So there is no need to create tables in the database manually.

But some times we need some alterations and removes some fields from Domain class.When we remove any field from Doman class and run server it will not removed field from actual table.

Hibernate provides some configuration for handle this type of situation.

When we use hibernate with SpringBoot we configure it in application.properties file.

spring.jpa.hibernate.ddl-auto=none

When we use "none" in our application hibernate not performed any ddl command on application startup.

We maintained a sql script file of tables which are created in our application. Script file contains create table and constraints query of all tables which are used in domain.

Before run application we simply run sql script file.Script file create all tables in database.

When application run it will not create any tables beacuse we set auto table creation none.

Now we can perfomed any operation on database.Like insert data in a table,update any data of table and many more.

Example of Script File ==>

When any requirement comes to remove any fields which are not required in curren scenario.

1. Simply create another script file with name of table on which action perfomed.

    Example => If we want to remove "active" field from role table. Simply we create a script file with named "Role_Script.sql".

    Write alter table sql query for remove active fields from table.Query will be.

    "alter table role DROP COLUMN active"

2. Add description regarding table updation.Description add in comments section of Role_Script.sql file.

   /*

    Role table updated at 20 august 2017 by prakhar verma

  */

3. This comment helped when we again performed any alter operation on role table.

About Author

Author Image
Prakhar Verma

Prakhar is a Web App Developer. Experienced in Java and always gives his best effort to complete the given task. He is self motivated and fun loving person.

Request for Proposal

Name is required

Comment is required

Sending message..