I’m trying to use a NativeQuery to return a list of entity objects. The class has several lazy-loaded collections that I want populated.
My code looks like the following:
EntityGraph entityGraph = entityManager.createEntityGraph(clazz); entityGraph.addAttributeNodes("collection1","collection2"); Query query = entityManager.createNativeQuery(sql, clazz).setHint("javax.persistence.fetchgraph",entityGraph); items = query.getResultList();
When I run the code I get the following error:
java.lang.ClassCastException: org.hibernate.internal.SQLQueryImpl cannot be cast to org.hibernate.internal.QueryImpl
Since a native query can return a list of managed objects, I think it’s reasonable to expect this code to work.
Is there a way to make this work? The query isn’t expressible as a JPQL query, so if this can’t work I may be stuck using straight JDBC which is a shame.