18

I want to create database schema in hibernate first time. And further, if there is any modification in the schema, like addition of a new table or deletion of some column, I want to update the existing schema keeping the previous data intact.

As per options given at this question, it looks like either I can create schema destroying the previous data, or I can update the schema.

Is there any value which can do both?

peterh
  • 11,875
  • 18
  • 85
  • 108
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • Do you want to automate this task? In other words, will you really be creating DB more than once? – Xorty Jan 11 '12 at 08:25
  • No i dont want to craete db once. As i said earlier, it should be created first time , then onwards it should the db schema if there is any modification. – M Sach Jan 11 '12 at 09:24
  • Have you tried by keeping 'update', Is 'update' not working in both cases?. – Jayasagar Nov 08 '13 at 05:12

3 Answers3

33

Actually I just checked <property name="hibernate.hbm2ddl.auto" value="update" /> is even creating tables for the first time and then later if table/schema exist it does update.

Update property is applicable when starting or adding a new model. You want to retain the earlier saved entity instances. This is the default schema creation style.

It tries to update the schema, if required. The following updates are supported:

See some of my observations

  • Add a field - A new column is added to the table.
  • Rename a field - A new column is added to the table, while the original column remains but is not used any longer. Note: The data from the old column is not migrated to the new column.
  • Remove a field - The column remains but is not used.
  • Change a field type - The type of the column does not change, which may result in type-mismatch exceptions.
  • Create an entity - Creates a new table.
  • Rename an entity - Creates a new table, while the original table remains.
  • Move an entity to another folder - Creates a new table, while the original table remains.
  • Delete an entity - The table remains.
peterh
  • 11,875
  • 18
  • 85
  • 108
Jayasagar
  • 2,046
  • 19
  • 22
4

You can use 'import.sql' .

Add a import.sql file in resource as follow:

/*create database at first time*/ 
CREATE SCHEMA your-database-name;

and add a line in 'hibernate.cfg.xml' as follow:

<hibernate-configuration>
    <session-factory>
       ...
       ...
       <property name="hbm2ddl.import_files">import.sql</property>
       ...
       ...
    </session-factory>
</hibernate-configuration>

So, if not exist the database, hibernate creates new db.

cguzel
  • 1,673
  • 2
  • 19
  • 34
  • 1
    There is more modern way to accomplish schema initialization: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-hibernate – Yan Burtovoy May 01 '19 at 05:32
3

I would not recommend updating the db schema based on the entity changes. Try to go with Flyway or Liquibase. You can find similar questions on stackoverflow eg. Hibernate/JPA DB Schema Generation Best Practices

Community
  • 1
  • 1
Kris
  • 5,714
  • 2
  • 27
  • 47
  • 1
    Hi kris.Thanks for reply.Actually i just want to know for now, Is it possible in hibernate to create database schema if schema does not exist otherwise update the schema if it is already exist and there are some modification in hbm file with some value for property hibernate.hbm2ddl.auto – M Sach Jan 11 '12 at 10:32