Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate hbm2ddl.auto update does not drop columns with mysql

Its adds new ones, but as far as I can see it does not drop the old ones ?

When I say old ones, I mean properties of entity objects that are now completely removed,where previously they were present and annotated with @column

Are my only options to drop the col manually or change the config value to create ? Neither of which are particularly charming.

Or something else ?

like image 871
NimChimpsky Avatar asked Aug 16 '11 14:08

NimChimpsky


People also ask

What is hibernate hbm2ddl auto create-drop?

hibernate.hbm2ddl.auto. Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop , the database schema will be dropped when the SessionFactory is closed explicitly. e.g. validate | update | create | create-drop.

What is the default value for hibernate hbm2ddl auto property?

hbm2ddl. auto defaults to Hibernate not doing anything. Save this answer.

What is hibernate DDL auto update?

hbm2ddl. auto is a hibernate configuration property. It is used to validate and exports schema DDL to the database when the SessionFactory is created. If we want to make use of it, we should have to pass the appropriate values to the hibernate.


2 Answers

For what it's worth, never EVER use hbm2ddl.auto on any live/production database.

Yes, it is "working as intended" that "update" doesn't drop any columns that are not referenced (probably to allow you to use "legacy" databases that have columns that are not used by your hibernate app, but may be used by external applications). However, in certain circumstances, hibernate can drop and recreate columns if, for instance, you change the datatype in your entity. That is one of the reasons you should never use it for any production system.

Personally, I would never trust an automated "black box" framework to handle changes to the datamodel in anything but strictly local/dev environments. I have always set it up so in the local dev environments, you may do create-drop. Once it's time to start promoting your app to central test/stage and then prod, all database changes are done by DBA:s with good old fashioned DDL scripts. Data is far too valuable to risk on a potential bug or unexpected behavior in hibernate (or any other ORM/automated framework). I even make sure that the database user configured in my applications doesn't even have create/drop/alter privileges in the database, just to prevent disasters happening due to bad configuration in hibernate.

So, to answer your question - if you want hibernate to always maintain your database reflecting your entities exactly, "create-drop" is your only option. Just don't ever use it on anything but local dev databases.

like image 102
pap Avatar answered Sep 30 '22 21:09

pap


I'd have a look into liquibase for keeping your database in sync with your enitities. Maybe a bit of an overkill but well worth it.

like image 25
Portree Kid Avatar answered Sep 30 '22 19:09

Portree Kid