Java Hibernate 命名策略更改表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39162976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate naming strategy changing table names
提问by
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.
我对 hibernates(5.1 版)命名策略有点困惑 - 即它改变了我的表名,我想避免这种情况。另外 -spring.jpa.hibernate.naming_strategy
根据 intelij 似乎已弃用,但我找不到正确配置它的(另一种)方法。
I have the following configuration in application.properties:
我在 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.
表名就像在@Table(name = "")
usaUploadTable 中一样。
Now when I run my application, I get
现在当我运行我的应用程序时,我得到
Table 'usertable201.usa_upload_table' doesn't exist
表 'usertable201.usa_upload_table' 不存在
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?
我该怎么做才能让 hibernate 正确使用我的表名?
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
采纳答案by
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
:
问题在于 spring-boot-1.4 - 似乎他们已经更改了属性(或其他)我现在找到了这个答案改进的命名策略不再在 Hibernate 5 中工作,但它仍然没有正确解析。所以我稍微改变了代码,不使用下划线方法并扩展新引入的类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
并且在application.properties
我已将弃用的行更改为
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.
现在它完全使用我在实体文件中拥有的表名和列名。
回答by rsinha
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
it worked for me. here are the versions i'm using:
它对我有用。这是我正在使用的版本:
Spring Boot (v1.4.2.RELEASE)
Hibernate Core {5.0.11.Final}
回答by B?ng Rikimaru
For one who wants to upper case in Postgresql and Spring boot 1.5.2
对于想要在 Postgresql 和 Spring boot 1.5.2 中使用大写的人
public class CustomDatabaseIdentifierNamingStrategy extends org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implements Serializable {
public static final long serialVersionUID = 1L;
public static final CustomDatabaseIdentifierNamingStrategy INSTANCE = new CustomDatabaseIdentifierNamingStrategy();
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(name.getText().toUpperCase(), true);
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return new Identifier(name.getText().toUpperCase(), true);
}
}
回答by Igor Azarny
Spring boot 2.0.0 & Hibernate 5
Spring Boot 2.0.0 & Hibernate 5
public class MySqlNamingStrategyImpl extends org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implements Serializable {
protected boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) {
return false;
}
}
}
回答by Raj Kundalia
If you are providing @Table and @Column annotation in your entity classes with names provided with an underscore i.e. user_id i.e. @Column(name="user_id"), it will take the column name as user_id; if you give it as userid then it will change to user_id if you use no strategy or implicit strategy (specifically spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl). So, if you want a strategy where the entity attribute name changes to one with underscore and lowercase letters i.e. something from userId to user_id, you should use implicit or no strategy (which actually uses implicit strategy).
如果您在实体类中提供@Table 和@Column 注释,其名称带有下划线,即user_id ie @Column(name=" user_id"),它将采用列名作为user_id;如果您将其作为 userid 提供,那么如果您不使用策略或隐式策略(特别是 spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl),它将更改为 user_id。因此,如果您想要一种策略,其中实体属性名称更改为带有下划线和小写字母的名称,即从 userId 到 user_id 的内容,您应该使用隐式或不使用策略(实际上使用隐式策略)。
If you don't want your naming strategy to add an underscore to the column name or class name, then the strategy that you need to use would look like: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. The things that you provide in annotations @Table and @Column's name attribute would remain as it is.
如果您不希望命名策略在列名或类名中添加下划线,那么您需要使用的策略如下: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot .model.naming.PhysicalNamingStrategyStandardImpl。您在注释@Table 和@Column 的名称属性中提供的内容将保持原样。
If you don't want to provide annotations and want to manually handle the table name and column names, you should extend the class org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl and override the required methods. If you still use annotations for some of the cases here, remember the overridden methods will apply on the names written in those annotations. spring.jpa.hibernate.naming.physical-strategy=example.CustomStrategy
如果您不想提供注释并希望手动处理表名和列名,您应该扩展类 org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 并覆盖所需的方法。如果您仍然在此处的某些情况下使用注释,请记住重写的方法将应用于这些注释中写入的名称。spring.jpa.hibernate.naming.physical-strategy=example.CustomStrategy