0

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

Snake
  • 14,228
  • 27
  • 117
  • 250

1 Answers1

0

Your problem is that you are not injecting SessionFactory2 in the attribute. @Qualifier does nothing on its own, you need to add @Autowire

The solution is:

/** Hibernate Session factory. */
    @Autowire
    @Qualifier("SessionFactory2")
    private SessionFactory sessionFactory;

As addition, you don't need getter/setter to use @Autowired on a private attribute ;)

Jose Luis
  • 254
  • 2
  • 11
  • I added the autowire. It is still getting the first data source not the second one – Snake Apr 07 '19 at 21:04
  • Have you tried to remove the @Autowire in the setter? public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }. Remember that this method is not necessary (unless you use it explictly for anything else) – Jose Luis Apr 08 '19 at 07:01
  • Bu that wouldn't make any difference though – Snake Apr 08 '19 at 14:10
  • Ok, I saw it, the problem is that you are using @Transactional(). Using transactional involves the transaction manager which you have configured with "sessionFactory". You should remove @Transactional from the query method (for a single query it's not necessary). If you required further transactional control I guess you will need to use two TxManager. Here you are an example: https://stackoverflow.com/questions/4423125/spring-is-it-possible-to-use-multiple-transaction-managers-in-the-same-applica. – Jose Luis Apr 09 '19 at 06:49