Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Hibernate to access an Oracle DBLink'ed table

My user wants to input random sql to access databases for which a db link exists in Oracle db. However, this gives an exception with Hibernate. Below is a function which is supposed to check whether the query is valid by running the query. Any recommendation is welcome.

The exception is:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query 'Select ID,NAME from YYY@XXX_DB_LINK'

Here is the 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<Object[]> list = query.getResultList();
    } catch(Exception e) {
        return e.toString() +  " '" + sqlQuery + "'";
    }

    return null;
}
like image 300
Adder Avatar asked Nov 23 '25 16:11

Adder


1 Answers

You should create a SYNONYM for your linked table YYY@XXX_DB_LINK, then your user can submit a native query like Select ID,NAME from YYYwithout worrying about table real location.

like image 154
Matteo Baldi Avatar answered Nov 26 '25 06:11

Matteo Baldi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!