经过一些研究,参考这些帖子,我尝试设置自定义命名策略
Hibernate命名策略
带有错误下划线的JPA列
这是我的自定义命名策略
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
@Override
public Identifier toPhysicalCatalogName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
@Override
public Identifier toPhysicalColumnName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
@Override
public Identifier toPhysicalSchemaName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
@Override
public Identifier toPhysicalSequenceName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
@Override
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return convertToSnakeCase(identifier);
}
private Identifier convertToSnakeCase(final Identifier identifier) {
final String regex = "([a-z])([A-Z])";
final String replacement = "$1_$2";
final String newName = identifier.getText().replaceAll(regex, replacement).toLowerCase();
return Identifier.toIdentifier(newName);
}
LocalContainerEntityManagerFactoryBean:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
localContainerEntityManagerFactoryBean.setDataSource(dataSource());
localContainerEntityManagerFactoryBean.setPackagesToScan(basePackage);
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
localContainerEntityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
localContainerEntityManagerFactoryBean.setJpaProperties(customProperties());
return localContainerEntityManagerFactoryBean;
}
customProperties((
Properties customProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect",
ResourceConfig.getProperties().getProperty(PropertyConstants.SPRING_JPA_HIBERNATE_DIALECT));
properties.setProperty("hibernate.hbm2ddl.auto", ddl);
properties.setProperty("hibernate.show_sql", showSQL);
properties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", metaDataDefaults);
properties.setProperty("hibernate.jdbc.lob.non_contextual_creation", contextualCreation);
properties.setProperty("hibernate.physical_naming_strategy", "com.config.CustomPhysicalNamingStrategy");
return properties;
}
但当启动springboot应用程序时,它会抛出以下异常:
Caused by: java.lang.NullPointerException
at com.config.CustomPhysicalNamingStrategy.convertToSnakeCase(CustomPhysicalNamingStrategy.java:47)
CustomPhysicalNamingStrategy 中的精确性
final String newName = identifier.getText().replaceAll(regex, replacement).toLowerCase();
我是错过了一些配置,还是做得不对?
使用弹簧套-2.2.4.释放
问题出在CustomPhysicalNamingStrategy:
应该是:
public class CustomPhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {
private static final long serialVersionUID = 1L;
public static final CustomPhysicalNamingStrategy INSTANCE = new CustomPhysicalNamingStrategy ();
@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());
}
protected static String addUnderscores(String name) {
final StringBuilder buf = new StringBuilder(name);
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().toLowerCase();
}
}
请找到下面的链接:
自定义物理命名策略