Is there a way to change the JPA fetch type on a method?

后端 未结 4 440
面向向阳花
面向向阳花 2020-12-09 10:16

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

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 10:51

    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?

提交回复
热议问题