小编典典

无法使用Spring Boot + GORM + Gradle设置NamingStrategy

spring-boot

我们正在使用带有GORM和Gradle的Spring Boot启动一个新项目。我已经能够为hibernate配置大多数属性,但是到目前为止,我还没有找到设置
命名策略 的正确方法。

尝试次数

我尝试在 application.properties中 设置各种属性,并添加文件 hibernate.properties
。我们正在使用自动配置,我看到在 HibernateGormAutoConfiguration 中发现了道具并添加了道具。

我也做了一些尝试来创建实体管理器和会话工厂bean,但是没有运气。

application.properties中的示例 (尝试所有排列):

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

src / main / resources / hibernate.properties中的示例:

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

启动应用程序并尝试加载实体时的日志记录和stacktrace:

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)

代码示例

不幸的是,在每个字段上覆盖字段名称不是可行的解决方案:

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

构建文件的节选如下所示:

.. // 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"
    ..
}

任何帮助将不胜感激!



阅读 494

收藏
2020-05-30

共1个答案

小编典典

Grails确实会使用该spring.hibernate.naming_strategy设置,但不会以您期望的方式使用它,并且保留了默认的命名策略。我不确定是什么原因。您可能想提出一个问题,与Grails团队讨论。

同时,可以通过调用来configureNamingStrategy以编程方式配置默认命名策略AbstractGrailsDomainBinder。例如:

@EnableAutoConfiguration
class Application {
    static void main(String[] args) {
        AbstractGrailsDomainBinder.configureNamingStrategy('DEFAULT', DefaultNamingStrategy)
        SpringApplication.run Application, args
    }
}
2020-05-30