In Hibernate 5 you can define query hints to the underlying database using org.hibernate.query.Query.html#addQueryHint. The SQL dialect being used is responsible to inject the appropriate text related to each hint defined in the query.
Unfortunately, if you read the sources of org.hibernate.dialect.MySQLDialect you'll notice that it does not extends the method org.hibernate.dialect.Dialect#getQueryHintString, and it does nothing by default.
You can try finding some alternative MySQL dialect that implements database query hinting about indexes or, if you're willing to get your hands dirty, you can extend one of the MySQL dialects and do it yourself:
public class MySqlExtendedDialect extends MySQLDialect {
public String getQueryHintString(String query, List<String> hints) {
/** define a format and read the hints, then patch the query appropriately **/
String modifiedQuery = query;
return modifiedQuery;
}
}
You can, for example, define a hint like "USE_INDEX_COL1_COL2" in the query. Then when your dialect find a "USE_INDEX_*" hint, it needs to do some basic SQL parsing and inject the right text in the right place.
Pure fun !