java - 物理命名策略已注册但在持久性时未触发
问题描述
物理命名策略已注册但在持久性期间未触发。
我正在尝试为我的 dropwizard hibernate bundle 配置 PhysicalNaming Strategy
public abstract class CustomHibernateBundle<T extends io.dropwizard.Configuration> extends ScanningHibernateBundle<T> {
protected CustomHibernateBundle(String pckg) {
this(pckg, new SessionFactoryFactory());
}
protected CustomHibernateBundle(String pckg, SessionFactoryFactory sessionFactoryFactory) {
this(new String[]{pckg}, sessionFactoryFactory);
}
protected CustomHibernateBundle(String[] pckgs, SessionFactoryFactory sessionFactoryFactory) {
super(pckgs, sessionFactoryFactory);
}
public void configure(Configuration configuration) {
super.configure(configuration);
configuration.setPhysicalNamingStrategy(new CustomNamingStrategy());
}
}
public class CustomNamingStrategy implements PhysicalNamingStrategy {
private String tableName(Identifier identifier) {
if (identifier == null)
return null;
String newName = identifier.getText();
String customID = (String) MDC.get("CUSTOM-ID");
if (!StringUtils.isEmpty(customID) && taint.equalsIgnoreCase("custom_id"))
newName = newName + "_custom";
return newName;
}
@Override
public Identifier toPhysicalCatalogName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return identifier;
}
@Override
public Identifier toPhysicalSchemaName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return identifier;
}
@Override
public Identifier toPhysicalTableName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return jdbcEnvironment.getIdentifierHelper().toIdentifier(tableName(identifier));
}
@Override
public Identifier toPhysicalSequenceName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return identifier;
}
@Override
public Identifier toPhysicalColumnName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
return identifier;
}
}
启动时,命名策略类正在正确注册。但是当我尝试坚持时,实际的命名策略并没有发挥作用。
我也尝试过使用
hibernate.naming.physical_strategy: com.someorg.CustomStrategy
在我的 YAML 文件中,但没有用。
解决方案
对我有用的是在配置上调用方法(而不是插入属性),
configuration.setPhysicalNamingStrategy(new ReallyCoolNamingStrategy());
以及财产:
hibernate.globally_quoted_identifiers: true
对于 DropWizard + Hibernate Web 服务器:
按照 DropWizard 的设置说明进行操作
将所需的设置放入您的配置中:
在这个片段中,我添加了命名策略。
对于 PostgreSQL,我有
globally quoted identifiers
asfalse
database:
driverClass: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/mule?currentSchema=public
user: mule-admin
password:
properties:
hibernate.dialect: org.hibernate.dialect.PostgreSQL10Dialect
hibernate.physical_naming_strategy: net.sf.zoftwhere.hibernate.SnakeCaseNamingStrategy
hibernate.globally_quoted_identifiers: false
hibernate.hbm2ddl.auto: update
hibernate.show_sql: false
hibernate.format_sql: false
更新 Hibernate Bundle 的初始化:
- 这里存储的属性用于硬编码策略。
public static <T extends DatabaseConfiguration> HibernateBundle<T> getHibernateBundle() {
return new HibernateBundle<>(AbstractEntity.class, persistenceEntities()) {
@Override
public DataSourceFactory getDataSourceFactory(T configuration) {
return configuration.getDataSourceFactory();
}
@Override
protected void configure(org.hibernate.cfg.Configuration configuration) {
final String namingStrategy = configuration.getProperty("hibernate.physical_naming_strategy");
if (SnakeCaseNamingStrategy.class.getName().equals(namingStrategy)) {
configuration.setPhysicalNamingStrategy(new SnakeCaseNamingStrategy());
} else if (MacroCaseNamingStrategy.class.getName().equals(namingStrategy)) {
configuration.setPhysicalNamingStrategy(new MacroCaseNamingStrategy());
}
}
};
}
public static Class<?>[] persistenceEntities() {
return new Class<?>[]{
Account.class,
AccessToken.class,
ShellSession.class,
};
}
- 仔细检查您的策略:
- 确保为空值返回空值。
- 确保传递
isQuoted
参数。
...
@Override
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
protected Identifier convertToSnakeCase(final Identifier identifier) {
if (identifier == null) {
return null;
}
return Identifier.toIdentifier(snakeCase(identifier.getText()), identifier.isQuoted());
}
protected String snakeCase(String input) {
return input.replaceAll("([a-z])([A-Z])", "$1_$2")
.replaceAll("([A-Z])([A-Z][a-z])", "$1_$2")
.toLowerCase();
}
...
希望这可以帮助。
DropWizard 1.3.13
Hibernate 5.4.3.Final
Java 11.0.2 (Oracle)
推荐阅读
- python - Kivy 返回空白屏幕
- windows - Windows 可执行文件的 Data Directory 条目和 Section Header 条目之间有什么区别?
- c# - 在 c# 中使用 vsphere-automation-sdk.net 从 VMWare 禁用 VM 的网络接口卡中的问题
- c# - 未检查系统 IP 地址,应用程序无法运行
- session - 为什么会话不适合 CORS?
- python - 如何判断我的 python 程序是否向 sys.stdout 写入了任何内容(在同一个程序中)?
- java - 为什么调试报告会在单击微调器时记录“尝试完成输入事件,但输入事件接收器已被释放”?
- java - 如何在布局中包含 listView 以及布局中的其他视图
- reactjs - Antdesign Datepicker 转换为 Typescript
- excel - 列范围 A 到 Z 的最后一个非空单元格