Hi everybody,
I am encountering an issue regarding the ineffectiveness of specific Oracle hints embedded in JPQL queries within a project that is migrating from Hibernate 3 to Hibernate 5 .
Problem Context: Our project extensively uses JPQL queries (not native SQL) that include Oracle hints (e.g., /*+ full(alias) */ , /*+ first_rows(N) */ ). For these hints to be effective, they must refer to the table alias generated by Hibernate in the final SQL.
During the migration from Hibernate 3 to Hibernate 5, we have observed that the table aliases generated by Hibernate for entities in JPQL queries have changed . Consequently, Oracle hints that referred to the old aliases have become ineffective, as the database cannot map the alias specified in the hint to the alias actually generated and used in the underlying SQL.
Concrete Example: Consider the following JPQL query:
@Query("SELECT /*+ first_rows full(target) */ t FROM MyEntity t WHERE t.id = :id")
List<MyEntity> getEntityFromId(@Param("id") Long id);
In this example, target is the alias we would like to use in the hint. However, if Hibernate generates a different alias (e.g., myentity0_. or generated_alias_0_ ) for MyEntity in the final SQL, the full(target) hint becomes null.
Request for Support / Proposed Solution: I know that it’s not the correct way to use hint in not-native JPQL, but I would like to know if there is a mechanism or an extension point in Hibernate 5 (or related libraries) that allows intercepting the JPQL to SQL translation process. The objective would be to:
- Identify the presence of Oracle hints within the JPQL query.
- Detect the alias generated by Hibernate for the entities involved in the query.
- Dynamically replace the “placeholder” alias in the hint (e.g.,
target) with the correct alias generated by Hibernate, before the query is sent to the database.
I am available to actively contribute to the development of this functionality, should it be deemed feasible and align with Hibernate’s roadmap or extensible architecture.