Hibernate改进的命名策略不起作用。

3

我有一个数据库,其中列使用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 的情况下使其工作?

2个回答

3

明确一点(因为我曾经花了一些时间从Maciej的答案中弄清楚),这对于我在Hibernate 5.x上有效:

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

2

jpa.hibernate.naming_strategy属性键仅在Spring Boot的配置中使用。

当您自己配置Hibernate时,应改用Hibernate属性。 Hibernate命名策略属性键的正确名称是hibernate.ejb.naming_strategy

请尝试在您的jpaProperties对象中替换它:

Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", environment.getProperty("jpa.hibernate.dialect")); //jpa dialect property key is different too
jpaProperties.put("hibernate.ejb.naming_strategy", "main.util.NamingStrategy");

所有杂项属性的列表如下: https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#configuration-misc-properties 我假设你只是删除了Spring Boot,但仍在使用Hibernate 4.x(在Hibernate 5.x中,NamingStrategy已被ImplicitNamingStrategy和PhysicalNamingStrategy替换)。
对于Hibernate 5.x,您应该使用org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNam‌​ingStrategy和默认的Hibernate的ImplicitNamingStrategyhttps://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Release-Notes#naming-strategy 此物理命名策略的配置键为:hibernate.physical_naming_strategy

我使用的是 Hibernate 5.2.2。你知道在 Hibernate 5.2.2 中 org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy 的类似物吗? - Kirill
2
对于 Hibernate 5.x,您应该使用 org.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 Marczuk
1
此物理命名策略的配置键为:hibernate.physical_naming_strategy - Maciej Marczuk
1
@MaciejMarczuk 这需要在答案中。 - wild_nothing

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接