1

I have recently started learning hibernate and I got the following doubts

Correct me if I am wrong.

  1. If hbm2ddl.auto is set to create, every time hibernate tries to interact with any table, that table will be dropped first.

  2. Because hibernate drops a table every time it interacts with it, if I want to update a record I cannot configure hbm2ddl value as create.

2 Answers2

0

The table is not dropped and recreated every time hibernate interacts with it -- it's dropped and recreated when the SessionFactory is initialized. This usually means when your application starts up. So if you are doing some tests where you want to start with a clean database each time the application runs, setting hbm2ddl.auto to "create" would be reasonable.

For more, see the community documentation and this previous question.

boris
  • 121
  • 1
  • 5
0

If you will configure value as create you can update a record , but when your application is down , and you will restart it all changes will disappear. So actually in this case its better to use update.

In case of update it only updates schema , you can define some schema sql file and use it on database side , and simply configure hbm2ddlauto as update.

Here are possible values of hbm2.ddlauto:

  • validate: validate the schema, makes no changes to the database.

  • update: update the schema.

  • create: creates the schema, destroying previous data.

  • create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.

Mykhailo Moskura
  • 2,073
  • 1
  • 9
  • 20