title: Dynamically create SessionFactory in Hibernate
body: <p>In my application, there are multiple databases for each client.I want to access database as per the client logged in my application.</p>
  
  <p>There are two options for that:<br>
  1) Using multiple SessionFactory entry in hibernate-config.xml file.<br>
  2) Duplicate hibernate pojo file.</p>
  
  <p>But I want to create LocalSessionFactoryBean,SessionFactory,TransactionManager all these at runtime. So how can I do this? <strong>I don't want to do multiple entries of SessionFactory and DataSource in hibernate-config.xml File.</strong></p>
  
  <p>I have tried below snippet. </p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  public class PersistenceHibernateConfig
  {
  
      private String  driverClassName;
  
      private String  url;
  
      String          hibernateDialect;
  
      boolean         hibernateShowSql;
  
      @Bean
      public LocalSessionFactoryBean alertsSessionFactoryBean()
      {
          final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(this.restDataSource());
          sessionFactory.setPackagesToScan(new String[] { "com.ezdi.cac.bean.table" });
          sessionFactory.setHibernateProperties(this.hibernateProperties());
  
          return sessionFactory;
      }
  
      @Bean
      public DataSource restDataSource()
      {
          final DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(this.driverClassName);
          dataSource.setUrl(this.url);
          dataSource.setUsername("root");
          dataSource.setPassword("root");
  
          return dataSource;
      }
  
      @Bean
      public HibernateTransactionManager transactionManager()
      {
          final HibernateTransactionManager txManager = new HibernateTransactionManager();
          txManager.setSessionFactory(this.alertsSessionFactoryBean().getObject());
  
          return txManager;
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslationPostProcessor()
      {
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
      @Bean
      public PersistenceExceptionTranslator exceptionTranslator()
      {
          return new HibernateExceptionTranslator();
      }
  
      final Properties hibernateProperties()
      {
          return new Properties()
          {
              {
                  this.put("persistence.dialect", PersistenceHibernateConfig.this.hibernateDialect);
                  this.put("hibernate.show_sql", PersistenceHibernateConfig.this.hibernateShowSql);
              }
          };
  
      }
  
      /**
       * @return the driverClassName
       */
      public String getDriverClassName()
      {
          return driverClassName;
      }
  
      /**
       * @param driverClassName the driverClassName to set
       */
      public void setDriverClassName(String driverClassName)
      {
          this.driverClassName = driverClassName;
      }
  
      /**
       * @return the url
       */
      public String getUrl()
      {
          return url;
      }
  
      /**
       * @param url the url to set
       */
      public void setUrl(String url)
      {
          this.url = url;
      }
  
      /**
       * @return the hibernateDialect
       */
      public String getHibernateDialect()
      {
          return hibernateDialect;
      }
  
      /**
       * @param hibernateDialect the hibernateDialect to set
       */
      public void setHibernateDialect(String hibernateDialect)
      {
          this.hibernateDialect = hibernateDialect;
      }
  
      /**
       * @return the hibernateShowSql
       */
      public boolean isHibernateShowSql()
      {
          return hibernateShowSql;
      }
  
      /**
       * @param hibernateShowSql the hibernateShowSql to set
       */
      public void setHibernateShowSql(boolean hibernateShowSql)
      {
          this.hibernateShowSql = hibernateShowSql;
      }
  
  }
  </code></pre>
  
  <p>but whenever I get SessionFactory through LocalSessionFactoryBean and also through HibernateTransactionManager,I got it null.I don't know the reason why did I get null? </p>