I am using Spring and Hibernate on websphere and trying to do query to the database. I have 2 datasources in 2 different projects that I am importing and my issue is that only one datasource I picked up. Here is the hibernate XML for each project:
project 1:
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DbSource1" />
<property name="mappingResources">
project 2:
<bean id="SessionFactory2" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DbSource2" />
<property name="mappingResources">
-- Also there is JDBC template for stored procedure
<bean id="retrievalService" class="xx.xx.xx.JDBCRetrievalService">
<property name="jdbcTemplate" ref="jdbcTemplate" /> </bean>
<bean id="transactionalService" class="xx.xx.xx.JDBCTransactionalService">
<property name="jdbcTemplate" ref="jdbcTemplate" /> </bean>
In my project application context, I simply import the 2 xmls above
<import resource="classpath:DBsource-hibernate1.xml" />
<import resource="classpath:DBSource-hibernate2.xml" />
Now in my code I have the following:
public abstract class HibernateBaseDao implements Serializable {
private static final long serialVersionUID = -8688473006487128511L;
/** Hibernate Session factory. */
@Qualifier("SessionFactory2")
private SessionFactory sessionFactory;
protected HibernateBaseDao() {
this.sessionFactory = null;
}
protected SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
return sessionFactory != null ? sessionFactory.getCurrentSession() : null;
}
}
@Component("MyDao")
public class MyDaoImpl
extends HibernateBaseDao
implements MyDaoI{
@Override
public int getValueFromDatabase(){
SQLQuery sqlQuery = (SQLQuery) createSQLQuery("select uservalue from MyTable");
List<Integer> values= sqlQuery.list();
values.get(0);
}
}
And finally here is my code that calls it:
@Autowired MyDao myDao;
@Override
@Transactional()
public void runQuery() throws Exception
{
myDao.getValueFromDatabase
}
The problem that it returns saying no table exists. After investigating, I found out that it is picking the first datasource. I have been pulling my hair for 2 days because of this. Any suggestions??? I already have qualifier for SessionFactory2 in the base class that I am extending
Thank you so much