title: Use Hibernate to access an Oracle DBLink'ed table
body: <p>My user wants to input random sql to access databases for which a db link exists in <code>Oracle</code> db. However, this gives an exception with <code>Hibernate</code>.
  Below is a function which is supposed to check whether the query is valid by running the query.
  Any recommendation is welcome.</p>
  
  <p>The exception is: </p>
  
  <pre><code>javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query 'Select ID,NAME from YYY@XXX_DB_LINK'
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>public String testSQL(String sqlQuery) {
      if(!checkAllowedSQL(sqlQuery)) {
          logger.debug("Forbidden SQL Query detected: " + sqlQuery);
          return "Forbidden SQL Query detected '" + sqlQuery + "'";
      }
  
  
  try {
      Query query = entityManager.createNativeQuery(sqlQuery);
          List&lt;Object[]&gt; list = query.getResultList();
      } catch(Exception e) {
          return e.toString() +  " '" + sqlQuery + "'";
      }
  
      return null;
  }
  </code></pre>