Saturday, August 16, 2008

Hibernate's hbm2ddl automagic and why you shouldn't use it on your agile project

Hibernate now has a new1 feature: hbm2ddl. That stands for Hibernate Mappings to Data Definition Language. Which means that hibernate will read the mapping files, figure out what kind of a database it is connecting to (SQL Server, Oracle, ...) and automagically generate and execute the appropriate DDL commands required to create the correct schema. So no more hassle of writing and maintaining dialect specific SQL scripts for each kind of database that you need your application to work with. What's not to like about that, you ask?

Well, here is what I don't like. hbm2ddl assumes that it is executing against a clean schema. If the database has an existing schema or data, it WILL blow those away. Not good at all. Especially not good in an agile project which updates schemas like so. Bottom line: I'll stick to using my update scripts for now, thank you very much.

PS: Don't get me wrong, I love hibernate most of the time. It's a great tool for mapping objects to the DB without too much hassle. It makes life a lot easier most of the time. It's just that this particular feature caught me totally by surprise. And I hate these kind of surprises!

1: For a given value of new.