物理命名策略已注册,但在持久性过程中未触发。
我正在尝试为我的dropwizard休眠包配置PhysicalNaming策略
table.put_item(Item=jsonDict['test'])
在启动时,命名策略类已正确注册。但是,当我尝试坚持时,实际的命名策略并未生效。
我也尝试使用
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;
}
}
在我的YAML文件中,但没有用。
答案 0 :(得分:0)
对我有用的是在配置上调用方法(与插入属性相反),
configuration.setPhysicalNamingStrategy(new ReallyCoolNamingStrategy());
以及属性:
hibernate.globally_quoted_identifiers: true
对于DropWizard + Hibernate Web服务器:
按照DropWizard 的设置说明
在配置中放入所需的设置:
在此代码段中,我添加了命名策略。
对于PostgreSQL,我的globally quoted identifiers
为false
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捆绑包的初始化:
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)