Default naming strategies with Grails an plain Hibernate

Last year (damn so long ago? seems like last week) I wrote a post about naming strategies and reusing an existing Hibernate domain model in a Grails app. I stumbled across this because I created a Grails application that is used as a “back office” management application for our time tracking product the uses the existing Hibernate domain model classes from the Spring application. There has also been a blog post on the official Springsource blog about this where I made a comment.

Lately I tried to upgrade the Grails application from 1.3.4 to a never version. I tried version 1.3.5 last year and version 1.3.6 later. Both made problem with the naming strategy reoccur. I had no time to look into the details but today I gave it another try with the newest Grails release 1.3.7. It again modified my existing database and created tables and columns with a different naming strategy (having dbCreate set to update).

I found out that this issue and the patch for it caused the change in the behavior between 1.3.4 and 1.3.5. As you can see from the patch the (which has to be configured in Grails to use existing Hibernate domain classes) looks for a naming_strategy config setting. The Grails documentation says you have to configure hibernate.naming_strategy for GORM which means you have to define two values if you want to have the same naming strategy for GORM and the existing domain classes.

If the values are not used Grails uses the ImprovedNamingStrategy for both GORM and the Hibernate domain classes. By debugging into the Hibernate configuration of my project I found out that using Hibernate Annotations automatically configures the EJB3NamingStrategy. So I had to configure this strategy both for hibernate.naming_strategy and naming_strategy. To make it clearer I also configured it explicitly in my Spring project:

<bean id="sessionFactory" class="o.s.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	<property name="namingStrategy" ref="ejb3NamningStrategy" />
<bean id="ejb3NamningStrategy"  class="org.hibernate.cfg.EJB3NamingStrategy"/>