MariaDB的休眠命名策略(UPPER_SNAKE_CASE) [英] Hibernate naming strategy for MariaDB (UPPER_SNAKE_CASE)
问题描述
在我的应用程序中,我正在使用flyway进行数据迁移,因此,我在UPPER_SNAKE_CASE
中定义了所有具有所有名称的表(例如,用户-> USERS
,候选人组-> CANDIDATE_GROUP
).我为不同的环境提供了2种配置:本地和docker.在本地环境中,我正在运行h2,在Docker中,我正在与MariaDB运行.
In my application I'm using flyway for data migration, therefore I have defined all tables with all names in UPPER_SNAKE_CASE
(ex. users -> USERS
, candidateGroups -> CANDIDATE_GROUP
). I have provided 2 configurations for different environments: local and docker. On local environment I'm running h2, on Docker I'm running with MariaDB.
现在在h2上运行时一切正常,在MariaDB休眠状态下尝试对所有小写表执行操作,这将导致错误:
Now while running on h2 everything works fine, on MariaDB hibernate is attempts to perform operations on all lower case tables, this results in error:
由以下原因引起:java.sql.SQLException:表'application.users'不存在
Caused by: java.sql.SQLException: Table 'application.users' doesn't exist
经过一些调查,我尝试将其添加到我的application-docker.yml
:
After some investigations i have tried adding to my application-docker.yml
:
spring:
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
这会导致休眠状态下的表名大写:
Caused by: java.sql.SQLException: Table 'application.Users' doesn't exist
This results in capitalization of table names in hibernate:
Caused by: java.sql.SQLException: Table 'application.Users' doesn't exist
我知道我可以用注解@Table(name = "USERS")
命名所有表,并为所有列提供名称,但这不是我想要的解决方案.
I know I can name all tables with annotation @Table(name = "USERS")
and provide names for all columns as well, but this is not my desired solution.
不幸的是,我没有找到适合我情况的策略(我知道这显示了如何创建自定义命名策略,但我认为我的情况并不罕见),应该为我的MariaDB配置使用哪种命名策略来匹配我的情况?
Unfortunately I haven't found any strategy matching my case (I know this shows how to create custom naming strategy but I don't think my case is that uncommon), what naming strategy should I use for my MariaDB config to match my case?
推荐答案
在
There's no UPPER_SNAKE_CASE
naming strategy neither in Hibernate or Spring Data JPA, but one could define his/her own by extending PhysicalNamingStrategyStandardImpl or SpringPhysicalNamingStrategy.
在Spring Data JPA中,这很容易,因为Spring已经将名称转换为snake_case:
It is easier in Spring Data JPA as Spring already converts names to snake_case:
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import java.io.Serializable;
import java.util.Locale;
public class UpperSnakeCaseSpringPhysicalNamingStrategyImpl extends SpringPhysicalNamingStrategy implements Serializable {
@Override
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
name = name.toUpperCase(Locale.ROOT);
return new Identifier(name, quoted);
}
}
要实现Hibernate转换到snake_case的转换,并至少需要覆盖5种方法中的2种:
For Hibernate implementation of the conversion to snake_case and override of at least 2 of the 5 methods is required:
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 UpperSnakeCasePhysicalNamingStrategyImpl extends PhysicalNamingStrategyStandardImpl implements Serializable {
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return new Identifier(addUnderscores(name.getText()), name.isQuoted());
}
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return new Identifier(addUnderscores(name.getText()), name.isQuoted());
}
private static String addUnderscores(String name) {
final StringBuilder buf = new StringBuilder(name.replace('.', '_'));
for (int i = 1; i < buf.length() - 1; i++) {
if (
Character.isLowerCase(buf.charAt(i - 1)) &&
Character.isUpperCase(buf.charAt(i)) &&
Character.isLowerCase(buf.charAt(i + 1))
) {
buf.insert(i++, '_');
}
}
return buf.toString().toUpperCase(Locale.ROOT);
}
}
最后,应在application.properties或application.yml中设置自定义命名策略:
Finally the customized naming strategy should be set in application.properties or application.yml:
spring.jpa.hibernate.naming.physical-strategy=my.package.UpperSnakeCaseSpringPhysicalNamingStrategyImpl
有关SO的更多信息:
- change Hibernate 5 naming strategy + spring boot + annotation
- https://stackoverflow.com/questions/32437202
更新(2020年1月16日)
这篇关于MariaDB的休眠命名策略(UPPER_SNAKE_CASE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!