Hibernate 命名策略更改表名 [英] Hibernate naming strategy changing table names
问题描述
我对 hibernates(5.1 版)命名策略有点困惑 - 即它改变了我的表名,我想避免这种情况.另外 - spring.jpa.hibernate.naming_strategy
根据 intelij 似乎已被弃用,但我找不到正确配置它的(另一种)方法.
我在 application.properties 中有以下配置:
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategyspring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialectspring.jpa.properties.hibernate.enable_lazy_load_no_trans=truespring.jpa.properties.hibernate.current_session_context_class=thread
如上所述,第一个标记为已弃用.
现在我有一个实体:
@Entity@Table(name = "usaUploadTable", schema = "usertable201", catalog = "")公共类 UsaUploadTable {....}
表名就像在@Table(name = "")
usaUploadTable 中一样.
现在当我运行我的应用程序时,我得到 p><块引用>
表 'usertable201.usa_upload_table' 不存在
这是正确的 - 它没有像休眠如何改变它那样命名.
我该怎么做才能让 hibernate 正确使用我的表名?
我也试过
DefaultNamingStrategy改进的命名策略
他们都改了
版本:
spring-boot-1.4.0.RELEASE休眠 5.1javax-事务-api 1.2休眠验证器 5.2.4javassist 3.20
问题在于 spring-boot-1.4 - 似乎他们改变了属性(或其他)我现在找到了这个答案 ImprovedNamingStrategy 在 Hibernate 5 中不再有效,但它仍然没有正确解析.所以我稍微修改了代码,不使用下划线方法并扩展新引入的类 SpringPhysicalNamingStrategy
:
package com.foo;导入 org.hibernate.boot.model.naming.Identifier;导入 org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;导入 org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;导入 java.io.Serializable;导入 java.util.Locale;公共类 RealNamingStrategyImpl 扩展 org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy 实现 Serializable {public static final PhysicalNamingStrategyImpl INSTANCE = new PhysicalNamingStrategyImpl();@覆盖公共标识符 toPhysicalTableName(标识符名称,JdbcEnvironment 上下文){返回新标识符(name.getText(), name.isQuoted());}@覆盖公共标识符 toPhysicalColumnName(标识符名称,JdbcEnvironment 上下文){返回新标识符(name.getText(), name.isQuoted());}}
在 application.properties
中,我已将弃用的行更改为
spring.jpa.properties.hibernate.physical_naming_strategy=.RealNamingStrategyImpl
现在它完全使用我的实体文件中的表名和列名.
I'm a little bit confused by hibernates (version 5.1) naming strategy - namely it changes my table name and I'd like to avoid that. Also - spring.jpa.hibernate.naming_strategy
seems to be deprecated according to intelij, but I can't find a (nother) way of configuring it correctly.
I have the following configuration in application.properties:
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.jpa.properties.hibernate.current_session_context_class=thread
The first one is marked as depracted, as said.
Now I have an entity:
@Entity
@Table(name = "usaUploadTable", schema = "usertable201", catalog = "")
public class UsaUploadTable {
....
}
The table name is, like in @Table(name = "")
usaUploadTable.
Now when I run my application, I get
Table 'usertable201.usa_upload_table' doesn't exist
which is correct - it isn't named like how hibernate is changing it.
What can I do to make hibernate use my table name correctly?
Edit:
I've also tried
DefaultNamingStrategy
ImprovedNamingStrategy
All of them change it
Versions:
spring-boot-1.4.0.RELEASE
hibernate 5.1
javax-transaction-api 1.2
hibernate-validator 5.2.4
javassist 3.20
The problem lies in spring-boot-1.4 - it seems like they have changed the properties (or whatever) I've now found this answer ImprovedNamingStrategy no longer working in Hibernate 5, but it still didn't resolve correctly. So I have changed the code a little to not use the underscore method and to extend the newly introduced class SpringPhysicalNamingStrategy
:
package com.foo;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import java.io.Serializable;
import java.util.Locale;
public class RealNamingStrategyImpl extends org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implements Serializable {
public static final PhysicalNamingStrategyImpl INSTANCE = new PhysicalNamingStrategyImpl();
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(name.getText(), name.isQuoted());
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return new Identifier(name.getText(), name.isQuoted());
}
}
And in application.properties
I've changed the deprecated line to
spring.jpa.properties.hibernate.physical_naming_strategy=<package>.RealNamingStrategyImpl
Now it uses exactly the table and column names as I have them in my entity files.
这篇关于Hibernate 命名策略更改表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!