使用Spring data jpa 时,表字段(user_id)与实体(userId)不能对应,需要添加以下配置
参考:https://www.jianshu.com/p/fc2c79814956
峰与下划线的转化 通用配置:
application.properties 配置
无修改命名
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
遇到大写加下划线"_" 的命名
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
如果项目中使用的表既有下划线,又有驼峰,而且还不想使用@Column一个个标识:
application.properties
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
ReceptionDBConfig.class
// 配置字段
protected Map<String, Object> jpaProperties() {
Map<String, Object> props = new HashMap<>();
props.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
props.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
return props;
}
@Bean(name = "receptionEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean kcEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("receptionDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.icil.elsa.milestone.query.entities.reception")
.persistenceUnit("reception")
.properties(jpaProperties()) // 引入属性配置
.build();
}
原文地址:https://www.cnblogs.com/wanthune/p/11104564.html
时间: 2024-11-09 03:49:29