gpt4 book ai didi

java - Hibernate ImprovedNamingStrategy 不起作用

转载 作者:行者123 更新时间:2023-11-30 07:59:49 25 4
gpt4 key购买 nike

我有一个数据库,其中的列使用蛇形命名,实体中的列以驼峰命名。该项目是使用 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,我如何才能完成这项工作?

最佳答案

明确地说(因为我不得不花一些时间从 Maciej 的回答中弄清楚),这对我适用于 Hibernate 5.x:

properties.put("hibernate.physical_naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");

关于java - Hibernate ImprovedNamingStrategy 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39001044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com