gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:41:45 27 4
gpt4 key购买 nike

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

尝试次数

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

我也做了一些尝试来创建实体管理器和 session 工厂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"
..
}

任何帮助将不胜感激!

此代码与此处询问的问题属于同一项目: Spring boot Jersey with groovy/gradle fails on startup

最佳答案

Grails确实使用了spring.hibernate.naming_strategy设置,但没有以您期望的方式使用它,并且保留了默认的命名策略。我不确定是什么原因。您可能需要raise an issue与Grails团队进行讨论。

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

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

关于hibernate - 无法使用Spring Boot + GORM + Gradle设置NamingStrategy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26711323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com