我有一个数据库,其中列使用snakecase命名法,而实体中的列使用camelcase命名法。该项目是使用spring boot
编写的,并具有以下配置:
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean mainEntityManagerFactory(
@Qualifier("mainDataSource") DataSource mainDataSource,
@Qualifier("mainJpaProperties") JpaProperties mainJpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(mainJpaProperties.isShowSql());
adapter.setDatabase(mainJpaProperties.getDatabase());
adapter.setDatabasePlatform(mainJpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(mainJpaProperties.isGenerateDdl());
Map<String, Object> jpaProperties = new HashMap<>();
jpaProperties.putAll(mainJpaProperties.getHibernateProperties(mainDataSource));
jpaProperties.put("jpa.hibernate.naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy");
return new EntityManagerFactoryBuilder(adapter, mainJpaProperties, persistenceUnitManager)
.dataSource(mainDataSource)
.packages(Employee.class)
.properties(jpaProperties)
.persistenceUnit("main")
.jta(false)
.build();
}
这是application.properties的内容:
jpa.hibernate.hbm2ddl.auto=none
jpa.hibernate.ddlAuto=none
jpa.hibernate.naming_strategy = org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy
jpa.generateDdl=false
jpa.showSql=false
jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
现在我无法使用Spring Boot,正在尝试转换配置:
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean mainEntityManagerFactory(
@Qualifier("mainDataSource") DataSource mainDataSource
) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(Boolean.valueOf(environment.getProperty("jpa.showSql")));
adapter.setDatabase(Database.POSTGRESQL);
adapter.setDatabasePlatform(environment.getProperty("jpa.hibernate.dialect"));
adapter.setGenerateDdl(Boolean.valueOf(environment.getProperty("jpa.generateDdl")));
Properties jpaProperties = new Properties();
jpaProperties.put("jpa.hibernate.dialect", environment.getProperty("jpa.hibernate.dialect"));
jpaProperties.put("hibernate.naming_strategy", "main.util.NamingStrategy");
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(mainDataSource);
factoryBean.setJpaVendorAdapter(adapter);
factoryBean.setPersistenceUnitName("main");
factoryBean.setPersistenceUnitManager(persistenceUnitManager);
factoryBean.setJpaProperties(jpaProperties);
factoryBean.setPackagesToScan(packageToScan);
factoryBean.afterPropertiesSet();
return factoryBean;
}
main.util.NamingStrategy
只是复制了 org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy
。
当我尝试运行它时,似乎这个命名策略不起作用,因为我遇到了一个错误:
org.hibernate.AnnotationException:
Unable to create unique key constraint (emp_address_id, year, yearNumber)
on table employeedata:
database column 'emp_address_id' not found.
Make sure that you use the correct column name which depends
on the naming strategy in use
(it may not be the same as the property name in the entity,
especially for relational types)
如何在不使用 spring boot
的情况下使其工作?
org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy
的类似物吗? - Kirillorg.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
和默认的 Hibernate 的 ImplicitNamingStrategy。https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Release-Notes#naming-strategy - Maciej Marczukhibernate.physical_naming_strategy
。 - Maciej Marczuk