Unable to set NamingStrategy using Spring Boot + GORM + Gradle
Asked Answered
M

1

1

We're starting a new project using Spring Boot with GORM and Gradle. I've been able to configure most properties for hibernate, but I have so far been unable to find the correct way to set the naming strategy.

Attempts

I've tried setting a variety of properties in application.properties and adding the file hibernate.properties. We're using auto-configuration, and I see props are discovered and added in HibernateGormAutoConfiguration.

I've also made some attempts creating the entity manager and session factory beans with no luck.

Examples from application.properties (trying all permutations):

spring.hibernate.hbm2ddl.auto=none # this works!!
# from now on none works
# I tried all permutations with combinations of
# *.hibernate[.ejb].* and *.naming_strategy/naming-strategy
spring.hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy
spring.hibernate.ejb.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy
spring.jpa.hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy
spring.properties.hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy
spring.jpa.properties.hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy
spring.gorm.hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy
spring.gorm.properties.hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy
gorm.hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy

Examples from src/main/resources/hibernate.properties:

hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy
hibernate.ejb.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy

Logging and stacktrace when starting application and trying to load entity:

2014-11-03 10:12:04.381  INFO 81729 --- [           main] org.hibernate.cfg.Environment            : HHH000205: Loaded properties from ... 
resource hibernate.properties: {hibernate.ejb.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy, hibernate.namingStrategy=org.hibernate.cfg.DefaultNamingStrategy, hibernate.bytecode.use_reflection_optimizer=false}


2014-11-03 10:09:28.825  WARN 81619 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 207, SQLState: 42S22
2014-11-03 10:09:28.825 ERROR 81619 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Invalid column name 'origin_marking'.
2014-11-03 10:09:28.839 ERROR 81619 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[jerseyServlet]        : Servlet.service() for servlet [jerseyServlet] in context with path [] threw exception [org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not extract ResultSet; bad SQL grammar [n/a]; nested exception is java.sql.SQLException: Invalid column name 'origin_marking'.] with root cause
    java.sql.SQLException: Invalid column name 'origin_marking'.
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)

Code examples

Unfortunately overriding field names on a per field basis is not a viable solution:

static mapping = {
    columns {
        originMarking column: 'originMarking'
    }
}

Excerpts from the build files look like this:

.. // main build file
buildscript {
    repositories {
        jcenter()
        maven { url "http://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.0.M2")
        classpath 'org.springframework:springloaded:1.2.0.RELEASE'
    }
}
..
apply plugin: 'spring-boot'
..
.. // domain build file
jar.baseName = 'domain'
dependencies {
    compile "org.grails:gorm-hibernate4-spring-boot:1.1.0.RELEASE",
            "joda-time:joda-time:2.5",
            'org.jadira.usertype:usertype.jodatime:2.0.1',
            "commons-dbcp:commons-dbcp:1.4",
            "net.sourceforge.jtds:jtds:1.2.7"
    runtime "com.h2database:h2"
}
..
.. // api build file
apply plugin: 'spring-boot'
jar.baseName = 'api'
dependencies {
    compile project(':domain')
    compile "org.springframework.boot:spring-boot-starter-jersey"
    ..
}  

Any help would be highly appreciated!!


This code is part of the same project as the question asked here: Spring boot Jersey with groovy/gradle fails on startup

Manymanya answered 3/11, 2014 at 9:31 Comment(5)
Have you tried the documented spring.jpa.hibernate.naming-strategy in the application.properties?Martymartyn
Yes. That's what I was trying to say (somewhat cryptically) with 'trying all permutations' along with the comment on the end of application.properties '# as well as with hibernate.ejb* and *.naming_strategy/naming-strategy'. I changed that part of my description, hopefully it made it clearer.Manymanya
Then I suspect that GORM is setting/overriding it internally or something like that.Martymartyn
I share your suspicion but have so far been unable to track down where the override (or something similiar) happens. Downgrading the spring-boot version is an alternative (to find out if this could be a bug in the current version), but because *-starter-jersey was new in the current version I was hoping to avoid doing this.Manymanya
I suspect the gorm internals but my GORM knowledge is far to little for that.Martymartyn
C
0

Grails does use the spring.hibernate.naming_strategy setting, but not in such a way that it's then used as you'd expect, and it leaves the default naming strategy untouched. I'm not sure what the reasons for that are. You might want to raise an issue to discuss it with the Grails team.

In the meantime, it's possible to configure the default naming strategy programatically by calling configureNamingStrategy on AbstractGrailsDomainBinder. For example:

@EnableAutoConfiguration
class Application {
    static void main(String[] args) {
        AbstractGrailsDomainBinder.configureNamingStrategy('DEFAULT', DefaultNamingStrategy)
        SpringApplication.run Application, args
    }
}
Chism answered 3/11, 2014 at 14:8 Comment(1)
Your solution worked as a charm @AndyWilkinson, much obliged. I'll raise an issue with the Grails team.Manymanya

© 2022 - 2024 — McMap. All rights reserved.