Search code examples
sql-serverhibernateplayframeworkhint

JPA Entity use index hint


Is it possible to specify a database index hint on a play framework Entity query.

My code looks like:

public static List<Transaction> findOnInactive(Date date) {
    return Transaction.find(
            "date = ? and account in ( select d.acctNb from account d "
                    + " where d.date = ? and (d.inactive = true or d.blocked = true)" 
                    + " group by d.acctNb )", date, date).fetch();
}

Running the generated query takes 20 sec. However running the same query manually with

select * from transaction with (INDEX(_dta_index_k1_1)) ...

only take 1 sec. Anyway I could specify the index hint in my JPA query?


Solution

  • You need to use native SQL query, something like this:

    return JPA.em().createNativeQuery(
        "select * from transaction with (INDEX(_dta_index_k1_1)) ...",
        Transaction.class).getResultList();