Adder
Adder

Reputation: 5868

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;
}

Upvotes: 3

Views: 4005

Answers (1)

Matteo Baldi
Matteo Baldi

Reputation: 5828

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.

Upvotes: 4

Related Questions