I really want to know more about the update, export and the values that could be given to hibernate.hbm2ddl.auto
I need to know when to use the update and when not? And what is the alternative?
These are changes that could happen over DB:
- new tables
- new columns in old tables
- columns deleted
- data type of a column changed
- a type of a column changed its attributes
- tables dropped
- values of a column changed
In each case what is the best solution?
There's also the undocumented value of "none" to disable it entirely.
I Think you should have to concentrate on the
this Class Makes Your Configuration Dynamic So it allows you to choose whatever suites you best...
Checkout [SchemaExport]
validate: validate the schema, do not change happens to the database. update: update the schema with currently execute query. create: creates new schema every time, and destroy previous data. create-drop: drop the schema when the application is stopped or SessionFactory is closed explicitly.
validate
: It validates the schema and makes no changes to the DB.Assume you have added a new column in the mapping file and perform the insert operation, it will throw an Exception "missing the XYZ column" because the existing schema is different than the object you are going to insert. If you alter the table by adding that new column manually then perform the Insert operation then it will definitely insert all columns along with the new column to the Table. Means it doesn't make any changes/alters the existing schema/table.
update
: it alters the existing table in the database when you perform operation. You can add or remove columns with this option of hbm2ddl. But if you are going to add a new column that is 'NOT NULL' then it will ignore adding that particular column to the DB. Because the Table must be empty if you want to add a 'NOT NULL' column to the existing table.In theory, you can set hibernate.hbm2ddl.auto=update to update your database with changes to your model, but I would not trust that on a production database. An earlier version of the documentation said that this was experimental, at least; I do not know the current status.
Therefore, for our production database, do not set hibernate.hbm2ddl.auto - the default is to make no database changes. Instead, we manually create an SQL DDL update script that applies changes from one version to the next.
If you don't want to use Strings in your app and are looking for predefined constants have a look at
org.hibernate.cfg.AvailableSettings
class included in the Hibernate JAR, where you'll find a constant for all possible settings. In your case for example: