1

Here I have added my code. Issue occurs in try block while I am trying fetch list of tables.

Database is MySql
Exception is : java.lang.IllegalArgumentException: node to traverse cannot be null!

public class DBOptimizationDAO extends HibernateDaoSupport {

private static final Log log  = LogFactory.getLog(DBOptimizationDAO.class);

public void optimizeAdapter(String year)
{
    List<com.ecw.adapterservice.beans.TransactionInbound> transactionInboundList = null;
    StringBuilder queries = new StringBuilder();
    try {       
        transactionInboundList = (List<com.ecw.adapterservice.beans.TransactionInbound>)super.getHibernateTemplate().find("from TransactionInbound where inboundTimestamp < '" + year+ "-01-01'order by 1 desc limit 2");

        //  Check if archive table exist or not
        List<Object> inboundObj = getHibernateTemplate().find("SHOW TABLES LIKE transaction_outbound");
        List<Object> outboundObj = getHibernateTemplate().find("SHOW TABLES LIKE 'transaction_outbound_archive'");

1 Answers1

0

The HibernateTemplate::find expects a HQL query in the string parameter and you are passing a native statement. You can do native stuff (queries, statements, etc) using the Session object returned by HibernateTemplate::getSession. To pass a native select query you then have Session::createSQLQuery

BUT do you really want to rely on database specific code to do this? There is a more elegant way to do it by using DatabaseMetaData::getTables. See this answer. And you can get an instance of DatabaseMetaData from a callback method of your HibernateTemplate.

Try this:

        Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        if(!session instaceof SessionImpl){
                    //handle this, maybe throw an exception
                }
        else {
            Connection con = (SessionImpl)session.connection();
            ...
        }       
wi2ard
  • 1,471
  • 13
  • 24