我想在我的spring启动项目中手动配置hibernate jpa,因为我需要引用2个数据源。其中一个JPA bean配置如下:
@Configuration
@EntityScan("com.channeljin.common.data.entities.user")
@EnableJpaRepositories(
entityManagerFactoryRef = "userEntityManagerFactory",
transactionManagerRef = "userTransactionManager",
basePackages = "com.channeljin.common.data.repo.user")
public class UserDatasourceConfig {
@Autowired
private UserDatasourceProperties properties;
@Bean
public DataSource userDatasource() {
PoolProperties poolProperties = new PoolProperties();
poolProperties.setDriverClassName("com.mysql.jdbc.Driver");
poolProperties.setUrl(properties.getUrl());
poolProperties.setUsername(properties.getUsername());
poolProperties.setPassword(properties.getPassword());
org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
ds.setPoolProperties(poolProperties);
return ds;
}
@Bean
public LocalContainerEntityManagerFactoryBean userEntityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(false);
Map<String, String> properties = new HashMap<>();
// properties.put("hibernate.implicit_naming_strategy",
// "org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl");
// properties.put("hibernate.physical_naming_strategy",
// "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
properties.put("hibernate.show_sql", "true");
LocalContainerEntityManagerFactoryBean factoryBean =
new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(userDatasource());
factoryBean.setPackagesToScan("com.channeljin.common.data.entities.user");
factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.getJpaPropertyMap().putAll(properties);
return factoryBean;
}
@Bean
public PlatformTransactionManager userTransactionManager() {
return new JpaTransactionManager(userEntityManagerFactory().getObject());
}
}
问题在于实体的camelCase字段类似于&#39; createTime&#39;不能被翻译成像蛇&#39; create_time&#39;在最终生成的SQL中。我不想使用@Column
注释,我更喜欢默认的命名策略方式。那么我应该使用什么命名策略?上面代码中的注释行似乎不起作用。
更新 :我用Google搜索,似乎org.hibernate.cfg.ImprovedNamingStrategy
会将camelCase更改为snake_case,但它会以Hibernate4为目标,但最新的Spring启动会使用Hibernate5。
答案 0 :(得分:3)
经过调试我发现我应该使用Spring的命名策略:org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
。这里的完整bean定义为copy&amp;贴人。
@Bean
public LocalContainerEntityManagerFactoryBean userEntityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(false);
Map<String, String> properties = new HashMap<>();
properties.put("hibernate.implicit_naming_strategy",
"org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy");
properties.put("hibernate.physical_naming_strategy",
"org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
properties.put("hibernate.show_sql", "true");
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(userDatasource());
factoryBean.setPackagesToScan("com.channeljin.common.data.entities.user");
factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.getJpaPropertyMap().putAll(properties);
return factoryBean;
}