Hibernate field naming issue with Spring Boot (naming strategy)

后端 未结 6 920
粉色の甜心
粉色の甜心 2020-11-29 09:31

Note that this code does work with plain Spring but not with Spring Boot(v1.3.3), is there something i\'m missing because this is imported from a spring app that works. The

6条回答
  •  孤街浪徒
    2020-11-29 09:53

    with the late version :

        spring-boot-starter-data-jpa: ➡ 1.5.2.RELEASE
        hibernate-core:5.0.12.Final
    

    this class

    PhysicalNamingStrategyStandardImpl
    

    needs to be extended and added to hibernate properties.

    Here is a full working version

        public class PhysicalNamingStrategyImpl extends PhysicalNamingStrategyStandardImpl implements Serializable {
    
        public static final PhysicalNamingStrategyImpl INSTANCE = new PhysicalNamingStrategyImpl();
    
        @Override
        public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
            String nameModified;
            // Do whatever you want with the name modification
            return new Identifier(nameModified, name.isQuoted());
        }
    
    }
    
        @Override
        public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
             String nameModified;
            // Do whatever you want with the name modification
            return new Identifier(nameModified, name.isQuoted());
        }
    

    Linking it to hibernate should be done like this when configuring the datasource.

    properties.put("hibernate.physical_naming_strategy", "my.Package.PhysicalNamingStrategyImpl");
    

    here is a full working version of datasource config

    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.orm.jpa.JpaTransactionManager;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    import javax.persistence.EntityManagerFactory;
    import javax.sql.DataSource;
    import java.util.HashMap;
    import java.util.Map;
    
    
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            entityManagerFactoryRef = "entityManagerFactory",
            basePackages = { "com.xxxxxx.repository" }
    )
    public class SharedDataSourceConfig {
    
        @Value("${startup.ddl-auto}")
        String hbm2ddl;
    
        @Primary
        @Bean(name = "dataSource")
        @ConfigurationProperties("spring.datasource.shared")
        public DataSource customerDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Primary
        @Bean(name = "entityManagerFactory")
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(
                EntityManagerFactoryBuilder builder,
                @Qualifier("dataSource") DataSource dataSource) {
            Map properties = new HashMap();
            properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
            properties.put("hibernate.physical_naming_strategy", "my.package.PhysicalNamingStrategyImpl");
            return builder
                    .dataSource(dataSource)
                    .packages(PackageScannerHelper.getPackagesToScan())
                    .persistenceUnit("shared")
                    .properties(properties)
                    .build();
        }
    
        @Primary
        @Bean(name = "transactionManager")
        public PlatformTransactionManager transactionManager(
                @Qualifier("entityManagerFactory") EntityManagerFactory
                        entityManagerFactory
        ) {
            return new JpaTransactionManager(entityManagerFactory);
        }
    }
    

提交回复
热议问题