Can't set JPA naming strategy after configuring multiple data sources (Spring 1.4.1 / Hibernate 5.x)

前端 未结 4 753
别跟我提以往
别跟我提以往 2020-12-24 05:45

I am using Spring Boot 1.4.1 which uses Hibernate 5.0.11. Initially I configured a data source using application.properties like this:

spring.da         


        
相关标签:
4条回答
  • 2020-12-24 06:03

    The same as @ewert answer can be gained using properties:

    # this works
    spring.jpa.properties.hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    spring.jpa.properties.hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
    
    # but that doesn't work
    spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
    spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    
    0 讨论(0)
  • 2020-12-24 06:07

    The only way I get this running properly with Spring-Boot 2+ was setting the following manually:

      @Bean(name = "myEmf")
      public LocalContainerEntityManagerFactoryBean sapEntityManagerFactory(
          EntityManagerFactoryBuilder builder, @Qualifier("myDataSource") DataSource dataSource) {
        return builder
            .dataSource(dataSource)
            .packages("my.custom.package")
            .persistenceUnit("myPu")
            .properties(getProperties())
            .build();
      }
    
      public Map<String, String> getProperties() {
       val props = new HashMap<String, String>();
    
        if (isTest()) {
          props.put("hibernate.hbm2ddl.auto", "create");
        } else {
          props.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL95Dialect");
        }
        return props;
      }
    
    0 讨论(0)
  • 2020-12-24 06:14

    I had the same problem and fixed it with the following code (adapted to the code in the question - for a single entity manager):

    protected Map<String, Object> jpaProperties() {
        Map<String, Object> props = new HashMap<>();
        props.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
        props.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
        return props;
    }
    
    @Primary
    @Bean(name = "defaultEntityManager")
    public LocalContainerEntityManagerFactoryBean defaultEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
        return builder
            .dataSource(auntDataSource())
            .packages(Aunt.class)
            .persistenceUnit("aunt")
            .properties(jpaProperties())
            .build();
    }
    
    0 讨论(0)
  • 2020-12-24 06:15

    If you are using SessionFactory you should use next lines to set naming strategies.

    sessionFactory.setImplicitNamingStrategy(SpringImplicitNamingStrategy.INSTANCE);
    sessionFactory.setPhysicalNamingStrategy(new SpringPhysicalNamingStrategy());
    
    0 讨论(0)
提交回复
热议问题