Is there a way to change the JPA fetch type on a single method without editing the entity object?
I have a shared ORM layer consisting of JPA entity
In JPA2 I use EntityGraphs, which allows you to define what related entities you want to retrieve:
https://docs.oracle.com/javaee/7/tutorial/persistence-entitygraphs002.htm https://docs.oracle.com/javaee/7/tutorial/persistence-entitygraphs003.htm
You create a NamedQuery as you did, and you attach a Hint with key javax.persistence.loadgraph
or javax.persistence.fetchgraph
. It will retrieve the related entities that you defined in the graph.
You can find the details of difference between "loadgraph" and "fetchgraph" here: What is the diffenece between FETCH and LOAD for Entity graph of JPA?