title: Multiple DataBases using Hibernate in Annotation Style
body: <p>There have been multiple discussions on this topic in the site but I am using annotations all across to create sessionFactory.</p>
  
  <pre><code>private SessionFactory sessionFactory;
  
  @Autowired
  public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
  </code></pre>
  
  <p>This is how hibernate.cfg.xml looks like</p>
  
  <pre><code>&lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;property name="hibernate.bytecode.use_reflection_optimizer"&gt;false&lt;/property&gt;
      &lt;property name="hibernate.cache.provider_class"&gt;org.hibernate.cache.HashtableCacheProvider&lt;/property&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;xxxxx&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;xxxxx&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;XXXX&lt;/property&gt;
      &lt;property name="hibernate.connection.characterEncoding"&gt;utf-8&lt;/property&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/property&gt;
      &lt;property name="hibernate.format_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.use_sql_comments"&gt;true&lt;/property&gt;
      &lt;mapping class="XXXX" /&gt;
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>This is applicationContext.xml</p>
  
  <pre><code>&lt;!-- add tomcat datasource instance to springs context --&gt;
  &lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName" value="java:comp/env/jdbc/XXXXDS" /&gt;
      &lt;property name="resourceRef" value="true" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>How do I achieve multiple DB integration in this case. One solution that I came across was to create multiple hibernate.cfg.xml and create another session factory for it.</p>
  
  <p>But I have two doubts, 1) How do I indicate in autowiring, which sessionFactory to pick, 2) I will have to duplicate lot of info across hibernate.xml, for example beans. Is there any cleaner approach for this issue?</p>