15

How does javax.persistence.fetchgraph differ from javax.persistence.loadgraph when providing an EntityGraph hint to a JPA 2.1 query? The documentation isn't really clear.

Daryl Banttari
  • 546
  • 3
  • 9

1 Answers1

20

When using fetchgraph all relationships are considered to be lazy regardless of annotation, and only the elements of the provided graph are loaded. This particularly useful when running reports on certain objects and you don't want a lot of the stuff that's normally flagged to load via eager annotations.

If you want to eagerly load entities that are normally loaded via lazy annotation, you may use loadgraph to add entities to the query results that would normally be loaded later, thereby avoiding specific N+1 cases. Relationships that were already flagged as eager will continue to be loaded as usual.

See https://docs.oracle.com/javaee/7/tutorial/persistence-entitygraphs001.htm

Daryl Banttari
  • 546
  • 3
  • 9