Unable to set NamingStrategy using Spring Boot + G

2019-03-02 02:50发布

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

1条回答
欢心
2楼-- · 2019-03-02 03:17

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
    }
}
查看更多
登录 后发表回答