spring - 在 Spring JPA Hibernate 中使用蛇形案例

标签 spring hibernate jpa

我不得不在我的 Spring Boot 项目中手动配置 hibernate jpa,因为我需要引用 2 个数据源。 JPA bean 配置之一如下:

@Configuration
@EntityScan("com.channeljin.common.data.entities.user")
@EnableJpaRepositories(
        entityManagerFactoryRef = "userEntityManagerFactory",
        transactionManagerRef = "userTransactionManager",
        basePackages = "com.channeljin.common.data.repo.user")
public class UserDatasourceConfig {

    @Autowired
    private UserDatasourceProperties properties;

    @Bean
    public DataSource userDatasource() {
        PoolProperties poolProperties = new PoolProperties();
        poolProperties.setDriverClassName("com.mysql.jdbc.Driver");
        poolProperties.setUrl(properties.getUrl());
        poolProperties.setUsername(properties.getUsername());
        poolProperties.setPassword(properties.getPassword());
        org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
        ds.setPoolProperties(poolProperties);
        return ds;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean userEntityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        Map<String, String> properties = new HashMap<>();
//        properties.put("hibernate.implicit_naming_strategy",
//                "org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl");
//        properties.put("hibernate.physical_naming_strategy",
//                "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        properties.put("hibernate.show_sql", "true");
        LocalContainerEntityManagerFactoryBean factoryBean =
                new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(userDatasource());
        factoryBean.setPackagesToScan("com.channeljin.common.data.entities.user");
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.getJpaPropertyMap().putAll(properties);
        return factoryBean;
    }

    @Bean
    public PlatformTransactionManager userTransactionManager() {
        return new JpaTransactionManager(userEntityManagerFactory().getObject());
    }
}

问题是在最终生成的 SQL 中,实体的像“createTime”这样的驼峰字段不会被转换为“create_time”这样的 snake_case 字段。我不想使用 @Column annotation,我更喜欢默认的命名策略方式。那么我应该使用什么命名策略呢?上面代码中的注释行似乎不起作用。

更新 :我用谷歌搜索,似乎 org.hibernate.cfg.ImprovedNamingStrategy将camelCase更改为snake_case,但它针对Hibernate4,但最新的Spring引导使用Hibernate5。

最佳答案

调试后我发现我应该使用 Spring 的命名策略:org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy .以及这里为复制和粘贴人员提供的完整 bean 定义。

@Bean
    public LocalContainerEntityManagerFactoryBean userEntityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        Map<String, String> properties = new HashMap<>();
        properties.put("hibernate.implicit_naming_strategy",
                "org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy");
        properties.put("hibernate.physical_naming_strategy",
                "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
        properties.put("hibernate.show_sql", "true");
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(userDatasource());
        factoryBean.setPackagesToScan("com.channeljin.common.data.entities.user");
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.getJpaPropertyMap().putAll(properties);
        return factoryBean;
    }

关于spring - 在 Spring JPA Hibernate 中使用蛇形案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46234152/

相关文章:

java - 如何在从 A "Super"接口(interface)扩展的接口(interface)方法上创建方面

java.lang.IllegalArgumentException : Could not resolve property name hourtoreserve from Property set for bean com. ti.project.vaadin.vaadinprojectti.Day

sql - 为什么在 JPA Hibernate 中更新查询;所有属性都在 SQL 中得到更新

java - 使用 Hibernate 性能优化在 startIPNum 和 endIPNum 范围内查找 IP 号码

java - Java/JPA 中类似 Django 的查询

java - spring加载xml文件

java - Spring 启动,JPA 错误 : "Error executing DDL via JDBC Statement"

java - org.hibernate.ObjectNotFoundException : No row with the given identifier exists: Single table query

JPA:实体 X 与其自身具有 OneToMany 关系。如何?

mysql - 非法参数异常 : Type cannot be null (in JAVA)