In one of my Grails consulting projects we tried to define database indices via the static mapping element in the domain classes. A team member tried it an told me that this does not work so I digged a bit deeper to find out what the problem was.
I created a simple test project, configured
hbm2ddl auto to update, like I am used to and let Grails create a MySQL database schema. It seemed lie my colleague was right, the defined indices were not created (using Grails 1.3.7). Doing some web research brought up this Stackoverflow posting suggesting that indices are only created when
hbm2ddl auto was set to create (or create-drop) – which seemed to be the case.
In my opinion this had to be a bug in hibernate but some searches on the hibernate.org website brought up a statement about this in the official FAQ:
The hibernate.hbm2ddl.auto=update setting doesn’t create indexes
SchemaUpdate is activated by this configuration setting. SchemaUpdate is not really very powerful and comes without any warranties. For example, it does not create any indexes automatically. Furthermore, SchemaUpdate is only useful in development, per definition (a production schema is never updated automatically). You don’t need indexes in development.
I do not agree with that, in our project it is necessary to have indices in development, otherwise we are not able to import the data we need for testing!
Luckily I found issue HHH-1012 in the Hibernate which states that this behavior was changed (fixed) in current hibernate versions – I tried it with Hibernate 3.6.4 and this version created indices even when the
hbm2ddl setting is configured with update. Grails 1.4 will be using 3.6.x of Hibernate to so it seems like this issue will be resolved soon for our project.