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.
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:
No unique bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: [sessionFactory01, sessionFactory02]
My spring-servlet.xml, where I've declared the bean is as:
<bean id="sessionFactory01"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="DB01dataSource" />
<property name="configLocation">
<value>classpath:DB01.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.db_stage.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="sessionFactory02"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="DB02dataSource" />
<property name="configLocation">
<value>classpath:DB02.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.db_prod.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager01"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory01" />
</bean>
<bean id="transactionManager02"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory02" />
</bean>
I have two configuration files for this named DB01.cfg.xml
and DB02.cfg.xml
. I thought it wasn't required here, and hence didn't post it.
This is how I create the sessionFactory object in my DAOImpl:
@Autowired
@Qualifier("sessionFactory01") SessionFactory sessionFactory01 = new Configuration().configure("DB01.cfg.xml").buildSessionFactory();
@Autowired
@Qualifier("sessionFactory02") SessionFactory sessionFactory02 = new Configuration().configure("DB02.cfg.xml").buildSessionFactory();
NOTE: I've tried using @Resource
in place of @Autowired
in my DAOImpl, still gives me the same error.