title: Duplicate Bean Error while connecting to two different DBs using Hibernate despite implementing both the beans
body: <p>Now before anyone starts jumping to conclusions and looks for other posts to mark this as a duplicate, let me just say this is a last ditch effort for me too. I tried whatever I could find and still the problem persists. </p>
  
  <p>So, getting to the problem. I'm trying to connect to two different Databases from my Spring-Hiberate application. Both are Oracle 11G Databases. But am getting the following error:</p>
  
  <pre><code>No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [sessionFactory01, sessionFactory02]
  </code></pre>
  
  <p>My spring-servlet.xml, where I've declared the bean is as:</p>
  
  <pre><code>    &lt;bean id="sessionFactory01"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="DB01dataSource" /&gt;
      &lt;property name="configLocation"&gt;
          &lt;value&gt;classpath:DB01.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.db_stage.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="sessionFactory02"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="DB02dataSource" /&gt;
      &lt;property name="configLocation"&gt;
          &lt;value&gt;classpath:DB02.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${jdbc.db_prod.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
      &lt;bean id="transactionManager01"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory01" /&gt;
  &lt;/bean&gt;
      &lt;bean id="transactionManager02"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory02" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>I have two configuration files for this named <code>DB01.cfg.xml</code> and <code>DB02.cfg.xml</code>. I thought it wasn't required here, and hence didn't post it.</p>
  
  <p>This is how I create the sessionFactory object in my DAOImpl:</p>
  
  <pre><code>@Autowired
  @Qualifier("sessionFactory01") SessionFactory sessionFactory01 = new Configuration().configure("DB01.cfg.xml").buildSessionFactory();
  
  @Autowired
  @Qualifier("sessionFactory02") SessionFactory sessionFactory02 = new Configuration().configure("DB02.cfg.xml").buildSessionFactory();
  </code></pre>
  
  <p>NOTE: I've tried using <code>@Resource</code> in place of <code>@Autowired</code> in my DAOImpl, still gives me the same error.</p>