Steve
Steve

Reputation: 8819

JPA native join fetch

It's well known that we can retrieve an entity and all its children in a single JPQL query using join fetch. However, I can't seem to get this working with a native query. I'm getting [Ljava.lang.Object; cannot be cast to com.myapp.SearchResult.

What's happening is that I have an @SqlResultSetMapping configuration with multiple @EntityResults. Some related entities are one-to-one. One of the relationships is one-to-many. Instead of JPA setting up a parent-child relationship, it's returning one list of objects per row, with each item in the list being of one of the defined entity types.

Any idea how to get this working as intended? Any examples of this case?

Upvotes: 5

Views: 5941

Answers (1)

James
James

Reputation: 18379

You can't join fetch in native queries in basic JPA.

If you are using EclipseLink, you can use the "eclipselink.join-fetch" Query hint to enable a join fetch on a native SQL query (without an SqlResultSetMapping only).

Upvotes: 3

Related Questions