title: Hibernate not creating PostgreSQL schema object using JPA
body: <p>I have the following code:</p>
  <pre><code>@Configuration
  public class HibernateConfig {
  
      @Autowired
      private DataSourceProperties dataSourceProperties;
  
      @Autowired
      private JpaProperties jpaProperties;
  
      @Bean
      JpaVendorAdapter jpaVendorAdapter() {
          return new HibernateJpaVendorAdapter();
      }
  
      @Bean
      @Scope(
          value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,
          proxyMode = ScopedProxyMode.TARGET_CLASS)
      public JdbcTemplate jdbcTemplate() {
          return new JdbcTemplate(customDataSource(dataSourceProperties));
      }
  
      @Bean
      public DataSource customDataSource(DataSourceProperties properties) {
  
          final CustomHikariDataSource dataSource = properties.initializeDataSourceBuilder().type(CustomHikariDataSource.class).build();
          if (properties.getName() != null) {
              dataSource.setPoolName(properties.getName());
          }
          
          return dataSource;
      }
  
  //    @Bean
  //    LocalContainerEntityManagerFactoryBean entityManagerFactory(
  ////        DataSource dataSource,
  //        MultiTenantConnectionProvider multiTenantConnectionProviderImpl,
  //        CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl
  //    ) {
  //
  //        Map&lt;String, Object&gt; jpaPropertiesMap = createJpaPropertiesMap(multiTenantConnectionProviderImpl, currentTenantIdentifierResolverImpl);
  //
  //        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  //
  //        factoryBean.setDataSource(customDataSource(dataSourceProperties));
  //        factoryBean.setPackagesToScan(&quot;com.sample.billing*&quot;);
  //        factoryBean.setJpaVendorAdapter(this.jpaVendorAdapter());
  //        factoryBean.setJpaPropertyMap(jpaPropertiesMap);
  //
  //        return factoryBean;
  //    }
  
  
      private Map&lt;String, Object&gt; createJpaPropertiesMap(MultiTenantConnectionProvider multiTenantConnectionProviderImpl,
                                                         CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl) {
          Map&lt;String, Object&gt; jpaPropertiesMap = new HashMap&lt;&gt;(jpaProperties.getProperties());
  
          jpaPropertiesMap.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
          jpaPropertiesMap.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl);
          jpaPropertiesMap.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl);
  
          jpaPropertiesMap.put(Environment.FORMAT_SQL, true);
          jpaPropertiesMap.put(Environment.SHOW_SQL, true);
  
          return jpaPropertiesMap;
      }
  }
  
  
  public class CustomHikariDataSource extends HikariDataSource {
  
      @Override
      public Connection getConnection() throws SQLException {
  
          Connection connection = super.getConnection();
          connection.setSchema(&quot;public&quot;);
          return connection;
      }
  }
  </code></pre>
  <p>The project runs fine, but I would like to select the schema based on the client. Then, I uncomment the bean in the first class which was provided earlier:</p>
  <pre><code>@Bean
      LocalContainerEntityManagerFactoryBean entityManagerFactory(
  //        DataSource dataSource,
          MultiTenantConnectionProvider multiTenantConnectionProviderImpl,
          CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl
      ) {
  
          Map&lt;String, Object&gt; jpaPropertiesMap = createJpaPropertiesMap(multiTenantConnectionProviderImpl, currentTenantIdentifierResolverImpl);
  
          LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  
          factoryBean.setDataSource(customDataSource(dataSourceProperties));
          factoryBean.setPackagesToScan(&quot;com.sample.billing*&quot;);
          factoryBean.setJpaVendorAdapter(this.jpaVendorAdapter());
          factoryBean.setJpaPropertyMap(jpaPropertiesMap);
  
          return factoryBean;
      }
  </code></pre>
  <p>With the bean uncommented, I get the error provided:</p>
  <pre><code>2020-11-24 18:03:24.602 ERROR 31729 --- [   scheduling-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation &quot;account&quot; does not exist
    Position: 515
  2020-11-24 18:03:24.603  INFO 31729 --- [   scheduling-1] c.m.b.m.c.d.TenantConnectionProvider     : Release connection for tenant public
  2020-11-24 18:03:24.608 ERROR 31729 --- [   scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task
  
  org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:259) ~[spring-orm-5.3.1.jar:5.3.1]
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233) ~[spring-orm-5.3.1.jar:5.3.1]
  </code></pre>
  <p>The <code>SQLGrammarException</code> error comes because tables in the schema are not created but tables do create without this bean perfectly.</p>
  <p>What's the issue here and how do I solve it?</p>