如何在 Spring Data JPA 中设置 Hibernate 命名策略

Posted

技术标签:

【中文标题】如何在 Spring Data JPA 中设置 Hibernate 命名策略【英文标题】:How to set Hibernate naming strategy in Spring Data JPA 【发布时间】:2016-01-26 09:10:34 【问题描述】:

我使用 Spring Data JPA 并使用多个数据库,所以我必须自己配置而不是使用 @HibernateJpaAutoConfiguration

public class TesterDbConfig 

    @Autowired(required = false)
    private PersistenceUnitManager persistenceUnitManager;


    @Bean
    public JpaProperties testerJpaProperties() 
        JpaProperties jpaProperties = new JpaProperties();
        return jpaProperties;
    

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "datasource.primary")
    public DataSource testerDataSource() 
        return (DataSource) DataSourceBuilder.create().type(DataSource.class).build();
    

    @Bean
    public LocalContainerEntityManagerFactoryBean testerEntityManager(
        JpaProperties testerJpaProperties) 
        EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(testerJpaProperties);
        return builder.dataSource(testerDataSource()).packages(Cabang.class).persistenceUnit("primary")
            .build();
    

    @Bean
    public JpaTransactionManager testerTransactionManager(EntityManagerFactory testerEntityManager) 
        return new JpaTransactionManager(testerEntityManager);
    

    private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(
        JpaProperties testerJpaProperties) 
        JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(testerJpaProperties);
        return new EntityManagerFactoryBuilder(jpaVendorAdapter, testerJpaProperties,
            this.persistenceUnitManager);
    

    private JpaVendorAdapter createJpaVendorAdapter(JpaProperties testerJpaProperties) 
        AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql(testerJpaProperties.isShowSql());
        adapter.setDatabase(testerJpaProperties.getDatabase());
        adapter.setDatabasePlatform(testerJpaProperties.getDatabasePlatform());
        adapter.setGenerateDdl(testerJpaProperties.isGenerateDdl());
        return adapter;
    

这样,每次创建模型时,我都必须在每个变量中使用@Column,因为在我的数据库中,我使用蛇形大小写,但在模型类中,我使用骆驼形大小写。

如何设置 Hibernate 命名策略?

【问题讨论】:

【参考方案1】:

您可以通过 JpaProperties 进行设置,您已经在配置中使用了它:

@Bean
public JpaProperties testerJpaProperties() 
    JpaProperties jpaProperties = new JpaProperties();
    JpaProperties.Hibernate hibernate = new JpaProperties.Hibernate();
    hibernate.setNamingStrategy(SpringNamingStrategy.class);
    jpaProperties.setHibernate(hibernate);
    return jpaProperties;

并添加

@Bean
public LocalContainerEntityManagerFactoryBean testerEntityManager(
    JpaProperties testerJpaProperties) 
    EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(testerJpaProperties);
    return builder.dataSource(testerDataSource())
        .packages(Cabang.class)
        .persistenceUnit("primary")
        .properties(Collections.singletonMap("hibernate.ejb.naming_strategy",testerJpaProperties.getHibernate().getNamingStrategy()))
        .build();

【讨论】:

它仍然给出错误 org.postgresql.util.PSQLException: ERROR: column ... does not exist 因为它可能没有转换为蛇形大小写。命名策略错误?还是实施? 看起来除了在 JpaProperties 中设置之外,我还必须在 LocalContainerEntityManagerFactoryBean .properties(Collections.singletonMap("hibernate.ejb.naming_strategy",testerJpaProperties.getHibernate().getNamingStrategy())) 我从 link 中的示例中添加它

以上是关于如何在 Spring Data JPA 中设置 Hibernate 命名策略的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring java 中设置存储库?

如何在 Spring Boot 中设置副本集?

如何在 JPA 中设置 CURRENT_TIMESTAMP 的默认值

如何使用hibernate jpa在内存数据库中设置h2?

Java JPA 错误;您试图从查询字符串中设置类型类数据预期类型 int 的值

Spring Data JPA 简单查询