Java Spring Boot JPA 在 TABLE 中插入带有 Hibernate 的大写名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28571848/
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
Spring boot JPA insert in TABLE with uppercase name with Hibernate
提问by carlj
i have a table entity mapped as :
我有一个表实体映射为:
@Entity
public class ItemsToRegister implements Serializable{
@Id
@Column(name = "ID_ITEM_TO_REGISTER")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
.....
When i try to insert new record in database, the table name was translated in lowercase as : items_to_register , but my table name is ITEMS_TO_REGISTER How can i fix my problem without change MySql configuration? (my.cnf)
当我尝试在数据库中插入新记录时,表名被翻译成小写: items_to_register ,但我的表名是 ITEMS_TO_REGISTER 如何在不更改 MySql 配置的情况下解决我的问题?(my.cnf)
I have in my application.properties file :
我的 application.properties 文件中有:
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
采纳答案by jasonleakey
On hibernate 5, it would be
在休眠 5 上,它将是
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
in your application.propertiesfile.
在您的application.properties文件中。
回答by Mikk
You can try:
你可以试试:
@Entity
@Table(name = "ITEMS_TO_REGISTER")
public class ItemsToRegister implements Serializable {
...
回答by John Thompson
You'll need to escape the table name with tics(`) to make it case sensitive.
您需要使用 tics(`) 对表名进行转义以使其区分大小写。
@Table(name = "`ITEMS_TO_REGISTER`")
回答by carlj
The solution is to add:
解决方法是添加:
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
to application.properties
到 application.properties
回答by SekharKari
As @jasonleakey suggested we can consider using naming-strategy as below.
正如@jasonleakey 建议的那样,我们可以考虑使用如下命名策略。
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
this tells Hibernate to generate SQL's as exactly as specified in the @Table (name=" ") or @Column(name=" "). All well.
这告诉 Hibernate 生成与 @Table (name=" ") 或 @Column(name=" ") 中指定的完全相同的 SQL。一切安好。
But keep in mind - while using PhysicalNamingStrategy without @Table, @Column annotations in the entity class, hibernate generates SQL using class name and variable names. Consider the below java class
但请记住 - 在实体类中使用没有 @Table、@Column 注释的 PhysicalNamingStrategy 时,hibernate 使用类名和变量名生成 SQL。考虑下面的java类
Class Employee {
private String firstName;
private String lastName;
}
then the generated sql would be,
那么生成的sql将是,
select employee0_.firstName,employee0_lastName from Employee employee0_;
Unfortunately this is not a great choice as typically we would have defined the columns in DB as FIRST_NAME and LAST_NAME and table name as EMPLOYEE. Had you not used PhysicalNamingStrategy the SQL would have been
不幸的是,这不是一个很好的选择,因为通常我们会将 DB 中的列定义为 FIRST_NAME 和 LAST_NAME,将表名定义为 EMPLOYEE。如果您没有使用 PhysicalNamingStrategy,SQL 将是
select employee0_.first_name,employee0_last_name from employee employee0_;
so it's really a choice between the below two options.
所以它真的是以下两个选项之间的选择。
- Use PhysicalStrategy and explicitly define all tables names/column names in java code with @Table and @Column annotations.
or - Define lowercase table name in db and let hibernate automatically generate table names/column names for us.
- 使用 PhysicalStrategy 并使用 @Table 和 @Column 注释在 java 代码中显式定义所有表名/列名。
或者 - 在db中定义小写的表名,让hibernate自动为我们生成表名/列名。
回答by mjassani
You can implement your own strategy and invoke it from application.properties:
您可以实现自己的策略并从 application.properties 调用它:
spring.jpa.hibernate.naming.physical-strategy=com.proto.CustomPhysicalNamingStrategy
Bellow an example that always capitalize the first letter
下面是一个总是大写第一个字母的例子
import java.io.Serializable;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy, Serializable {
/**
* Singleton access
*/
public static final CustomPhysicalNamingStrategy INSTANCE = new CustomPhysicalNamingStrategy();
@Override
public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment context) {
return capitalize(name);
}
@Override
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment context) {
return capitalize(name);
}
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return capitalize(name);
}
@Override
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment context) {
return capitalize(name);
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return capitalize(name);
}
private Identifier capitalize(Identifier name) {
if (name == null)
return null;
if (name.isQuoted())
return name;
String text = StringUtils.capitalize(name.getText());
return Identifier.toIdentifier(text);
}
}