This question came out of my attempts to unify the JPA EntityManager contract with the Hibernate Session contract. An example method where this comes into play is an overloaded method named createQuery
. Session inherits many forms of this method from its super contracts. Session then tries to extend EntityManager as well, so it then inherits the createQuery
forms defined by EntityManager.
When I try to use Session to create a query given an HQL/JPQL String the compiler complains saying that the call is ambiguous, that 2 methods in particular match:
Query createQuery(String queryString)
<T> Query<T> createQuery(CriteriaQuery<T> criteria)
The only solution I have found is to explicitly "override" each of these forms on Session itself. We surmise that somehow "unifies" the type parameter (<T>
) resolution. But I'd really like to understand the why behind this. Any thoughts? Any better way to resolve this besides explicitly overriding each form on Session?
Longer version... :)
Hibernate defines both a Session contract and a StatelessSession contract to users. Each define a common set of methods plus there own individual methods. Part of this common set of methods is the ability to create Query objects:
interface QueryProducer {
Query createQuery(String queryString);
...
}
What I am trying to accomplish overall is to have Hibernate's Session (but not its StatelessSession!) contract extend the JPA EntityManager. So in that regard Session inherits the QueryProducer
methods, but the EntityManager
methods as well, e.g.:
interface EntityManager {
Query createQuery(String queryString);
TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery);
...
}
It's whenever I try to use one of these commonly inherited methods that I see the problem warning about the method call being ambiguous between #createQuery(String)
and #createQuery(CriteriaQuery<T>)
.